fscanf_variation19.phpt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. --TEST--
  2. Test fscanf() function: usage variations - string formats with boolean
  3. --FILE--
  4. <?php
  5. /* Test fscanf() to scan boolean data using different string format types */
  6. $file_path = __DIR__;
  7. echo "*** Test fscanf(): different string format types with boolean data ***\n";
  8. // create a file
  9. $filename = "$file_path/fscanf_variation19.tmp";
  10. $file_handle = fopen($filename, "w");
  11. if($file_handle == false)
  12. exit("Error:failed to open file $filename");
  13. // array of boolean types
  14. $bool_types = array (
  15. true,
  16. false,
  17. TRUE,
  18. FALSE,
  19. );
  20. $string_formats = array( "%s",
  21. "%hs", "%ls", "%Ls",
  22. " %s", "%s ", "% s",
  23. "\t%s", "\n%s", "%4s",
  24. "%30s", "%[a-zA-Z0-9]", "%*s");
  25. $counter = 1;
  26. // writing to the file
  27. foreach($bool_types as $value) {
  28. @fprintf($file_handle, $value);
  29. @fprintf($file_handle, "\n");
  30. }
  31. // closing the file
  32. fclose($file_handle);
  33. // opening the file for reading
  34. $file_handle = fopen($filename, "r");
  35. if($file_handle == false) {
  36. exit("Error:failed to open file $filename");
  37. }
  38. $counter = 1;
  39. // reading the values from file using different string formats
  40. foreach($string_formats as $string_format) {
  41. // rewind the file so that for every foreach iteration the file pointer starts from bof
  42. rewind($file_handle);
  43. echo "\n-- iteration $counter --\n";
  44. while( !feof($file_handle) ) {
  45. try {
  46. var_dump(fscanf($file_handle,$string_format));
  47. } catch (ValueError $exception) {
  48. echo $exception->getMessage() . "\n";
  49. }
  50. }
  51. $counter++;
  52. }
  53. echo "\n*** Done ***";
  54. ?>
  55. --CLEAN--
  56. <?php
  57. $file_path = __DIR__;
  58. $filename = "$file_path/fscanf_variation19.tmp";
  59. unlink($filename);
  60. ?>
  61. --EXPECT--
  62. *** Test fscanf(): different string format types with boolean data ***
  63. -- iteration 1 --
  64. array(1) {
  65. [0]=>
  66. string(1) "1"
  67. }
  68. NULL
  69. array(1) {
  70. [0]=>
  71. string(1) "1"
  72. }
  73. NULL
  74. bool(false)
  75. -- iteration 2 --
  76. array(1) {
  77. [0]=>
  78. string(1) "1"
  79. }
  80. NULL
  81. array(1) {
  82. [0]=>
  83. string(1) "1"
  84. }
  85. NULL
  86. bool(false)
  87. -- iteration 3 --
  88. array(1) {
  89. [0]=>
  90. string(1) "1"
  91. }
  92. NULL
  93. array(1) {
  94. [0]=>
  95. string(1) "1"
  96. }
  97. NULL
  98. bool(false)
  99. -- iteration 4 --
  100. array(1) {
  101. [0]=>
  102. string(1) "1"
  103. }
  104. NULL
  105. array(1) {
  106. [0]=>
  107. string(1) "1"
  108. }
  109. NULL
  110. bool(false)
  111. -- iteration 5 --
  112. array(1) {
  113. [0]=>
  114. string(1) "1"
  115. }
  116. NULL
  117. array(1) {
  118. [0]=>
  119. string(1) "1"
  120. }
  121. NULL
  122. bool(false)
  123. -- iteration 6 --
  124. array(1) {
  125. [0]=>
  126. string(1) "1"
  127. }
  128. NULL
  129. array(1) {
  130. [0]=>
  131. string(1) "1"
  132. }
  133. NULL
  134. bool(false)
  135. -- iteration 7 --
  136. Bad scan conversion character " "
  137. Bad scan conversion character " "
  138. Bad scan conversion character " "
  139. Bad scan conversion character " "
  140. bool(false)
  141. -- iteration 8 --
  142. array(1) {
  143. [0]=>
  144. string(1) "1"
  145. }
  146. NULL
  147. array(1) {
  148. [0]=>
  149. string(1) "1"
  150. }
  151. NULL
  152. bool(false)
  153. -- iteration 9 --
  154. array(1) {
  155. [0]=>
  156. string(1) "1"
  157. }
  158. NULL
  159. array(1) {
  160. [0]=>
  161. string(1) "1"
  162. }
  163. NULL
  164. bool(false)
  165. -- iteration 10 --
  166. array(1) {
  167. [0]=>
  168. string(1) "1"
  169. }
  170. NULL
  171. array(1) {
  172. [0]=>
  173. string(1) "1"
  174. }
  175. NULL
  176. bool(false)
  177. -- iteration 11 --
  178. array(1) {
  179. [0]=>
  180. string(1) "1"
  181. }
  182. NULL
  183. array(1) {
  184. [0]=>
  185. string(1) "1"
  186. }
  187. NULL
  188. bool(false)
  189. -- iteration 12 --
  190. array(1) {
  191. [0]=>
  192. string(1) "1"
  193. }
  194. array(1) {
  195. [0]=>
  196. NULL
  197. }
  198. array(1) {
  199. [0]=>
  200. string(1) "1"
  201. }
  202. array(1) {
  203. [0]=>
  204. NULL
  205. }
  206. bool(false)
  207. -- iteration 13 --
  208. array(0) {
  209. }
  210. NULL
  211. array(0) {
  212. }
  213. NULL
  214. bool(false)
  215. *** Done ***