1--TEST-- 2Test fscanf() function: usage variations - char formats with chars 3--FILE-- 4<?php 5 6/* 7 Prototype: mixed fscanf ( resource $handle, string $format [, mixed &$...] ); 8 Description: Parses input from a file according to a format 9*/ 10 11/* Test fscanf() to scan different chars using different char format types */ 12 13$file_path = dirname(__FILE__); 14 15echo "*** Test fscanf(): different char format types with chars ***\n"; 16 17// create a file 18$filename = "$file_path/fscanf_variation26.tmp"; 19$file_handle = fopen($filename, "w"); 20if($file_handle == false) 21 exit("Error:failed to open file $filename"); 22 23// array of chars 24$char_types = array( 'a', "a", 67, -67, 99 ); 25 26$char_formats = array( "%c", 27 "%hc", "%lc", "%Lc", 28 " %c", "%c ", "% c", 29 "\t%c", "\n%c", "%4c", 30 "%30c", "%[a-zA-Z@#$&0-9]", "%*c"); 31 32$counter = 1; 33 34// writing to the file 35foreach($char_types as $char) { 36 @fprintf($file_handle, $char); 37 @fprintf($file_handle, "\n"); 38} 39// closing the file 40fclose($file_handle); 41 42// opening the file for reading 43$file_handle = fopen($filename, "r"); 44if($file_handle == false) { 45 exit("Error:failed to open file $filename"); 46} 47 48$counter = 1; 49// reading the values from file using different char formats 50foreach($char_formats as $char_format) { 51 // rewind the file so that for every foreach iteration the file pointer starts from bof 52 rewind($file_handle); 53 echo "\n-- iteration $counter --\n"; 54 while( !feof($file_handle) ) { 55 var_dump( fscanf($file_handle,$char_format) ); 56 } 57 $counter++; 58} 59 60echo "\n*** Done ***"; 61?> 62--CLEAN-- 63<?php 64$file_path = dirname(__FILE__); 65$filename = "$file_path/fscanf_variation26.tmp"; 66unlink($filename); 67?> 68--EXPECTF-- 69*** Test fscanf(): different char format types with chars *** 70 71-- iteration 1 -- 72array(1) { 73 [0]=> 74 string(1) "a" 75} 76array(1) { 77 [0]=> 78 string(1) "a" 79} 80array(1) { 81 [0]=> 82 string(1) "6" 83} 84array(1) { 85 [0]=> 86 string(1) "-" 87} 88array(1) { 89 [0]=> 90 string(1) "9" 91} 92bool(false) 93 94-- iteration 2 -- 95array(1) { 96 [0]=> 97 string(1) "a" 98} 99array(1) { 100 [0]=> 101 string(1) "a" 102} 103array(1) { 104 [0]=> 105 string(1) "6" 106} 107array(1) { 108 [0]=> 109 string(1) "-" 110} 111array(1) { 112 [0]=> 113 string(1) "9" 114} 115bool(false) 116 117-- iteration 3 -- 118array(1) { 119 [0]=> 120 string(1) "a" 121} 122array(1) { 123 [0]=> 124 string(1) "a" 125} 126array(1) { 127 [0]=> 128 string(1) "6" 129} 130array(1) { 131 [0]=> 132 string(1) "-" 133} 134array(1) { 135 [0]=> 136 string(1) "9" 137} 138bool(false) 139 140-- iteration 4 -- 141array(1) { 142 [0]=> 143 string(1) "a" 144} 145array(1) { 146 [0]=> 147 string(1) "a" 148} 149array(1) { 150 [0]=> 151 string(1) "6" 152} 153array(1) { 154 [0]=> 155 string(1) "-" 156} 157array(1) { 158 [0]=> 159 string(1) "9" 160} 161bool(false) 162 163-- iteration 5 -- 164array(1) { 165 [0]=> 166 string(1) "a" 167} 168array(1) { 169 [0]=> 170 string(1) "a" 171} 172array(1) { 173 [0]=> 174 string(1) "6" 175} 176array(1) { 177 [0]=> 178 string(1) "-" 179} 180array(1) { 181 [0]=> 182 string(1) "9" 183} 184bool(false) 185 186-- iteration 6 -- 187array(1) { 188 [0]=> 189 string(1) "a" 190} 191array(1) { 192 [0]=> 193 string(1) "a" 194} 195array(1) { 196 [0]=> 197 string(1) "6" 198} 199array(1) { 200 [0]=> 201 string(1) "-" 202} 203array(1) { 204 [0]=> 205 string(1) "9" 206} 207bool(false) 208 209-- iteration 7 -- 210 211Warning: fscanf(): Bad scan conversion character " " in %s on line %d 212NULL 213 214Warning: fscanf(): Bad scan conversion character " " in %s on line %d 215NULL 216 217Warning: fscanf(): Bad scan conversion character " " in %s on line %d 218NULL 219 220Warning: fscanf(): Bad scan conversion character " " in %s on line %d 221NULL 222 223Warning: fscanf(): Bad scan conversion character " " in %s on line %d 224NULL 225bool(false) 226 227-- iteration 8 -- 228array(1) { 229 [0]=> 230 string(1) "a" 231} 232array(1) { 233 [0]=> 234 string(1) "a" 235} 236array(1) { 237 [0]=> 238 string(1) "6" 239} 240array(1) { 241 [0]=> 242 string(1) "-" 243} 244array(1) { 245 [0]=> 246 string(1) "9" 247} 248bool(false) 249 250-- iteration 9 -- 251array(1) { 252 [0]=> 253 string(1) "a" 254} 255array(1) { 256 [0]=> 257 string(1) "a" 258} 259array(1) { 260 [0]=> 261 string(1) "6" 262} 263array(1) { 264 [0]=> 265 string(1) "-" 266} 267array(1) { 268 [0]=> 269 string(1) "9" 270} 271bool(false) 272 273-- iteration 10 -- 274array(1) { 275 [0]=> 276 string(1) "a" 277} 278array(1) { 279 [0]=> 280 string(1) "a" 281} 282array(1) { 283 [0]=> 284 string(2) "67" 285} 286array(1) { 287 [0]=> 288 string(3) "-67" 289} 290array(1) { 291 [0]=> 292 string(2) "99" 293} 294bool(false) 295 296-- iteration 11 -- 297array(1) { 298 [0]=> 299 string(1) "a" 300} 301array(1) { 302 [0]=> 303 string(1) "a" 304} 305array(1) { 306 [0]=> 307 string(2) "67" 308} 309array(1) { 310 [0]=> 311 string(3) "-67" 312} 313array(1) { 314 [0]=> 315 string(2) "99" 316} 317bool(false) 318 319-- iteration 12 -- 320array(1) { 321 [0]=> 322 string(1) "a" 323} 324array(1) { 325 [0]=> 326 string(1) "a" 327} 328array(1) { 329 [0]=> 330 string(2) "67" 331} 332array(1) { 333 [0]=> 334 NULL 335} 336array(1) { 337 [0]=> 338 string(2) "99" 339} 340bool(false) 341 342-- iteration 13 -- 343array(0) { 344} 345array(0) { 346} 347array(0) { 348} 349array(0) { 350} 351array(0) { 352} 353bool(false) 354 355*** Done *** 356 357