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