fgetss_variation5-win32.phpt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. --TEST--
  2. Test fgetss() function : usage variations - read/write modes, file pointer at EOF
  3. --SKIPIF--
  4. <?php
  5. if (substr(PHP_OS, 0, 3) != 'WIN') {
  6. die('skip.. only valid for Windows');
  7. }
  8. ?>
  9. --FILE--
  10. <?php
  11. /*
  12. Prototype: string fgetss ( resource $handle [, int $length [, string $allowable_tags]] );
  13. Description: Gets line from file pointer and strip HTML tags
  14. */
  15. /* try fgetss on files which are opened in read/write modes
  16. w+, w+b, w+t,
  17. a+, a+b, a+t,
  18. x+, x+b, x+t
  19. */
  20. echo "*** Testing fgetss() : usage variations ***\n";
  21. /* string with html and php tags */
  22. $string_with_tags = <<<EOT
  23. <test>Testing fgetss() functions</test>
  24. <?php echo "this string is within php tag"; ?> {;}<{> this
  25. is a heredoc string. <pg>ksklnm@@$$&$&^%&^%&^%&</pg>
  26. <html> html </html> <?php echo "php"; ?>
  27. this line is without any html and php tags
  28. this is a line with more than eighty character,want to check line splitting correctly after 80 characters
  29. this text contains some html tags <body> body </body> <br> br </br>
  30. this is the line with \n character.
  31. EOT;
  32. if(substr(PHP_OS, 0, 3) == "WIN") {
  33. $string_with_tags = str_replace("\r",'', $string_with_tags);
  34. }
  35. $filename = dirname(__FILE__)."/fgetss_variation5.tmp";
  36. /* try reading the file opened in different modes of reading */
  37. $file_modes = array("w+","w+b", "w+t","a+", "a+b", "a+t","x+","x+b","x+t");
  38. for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
  39. echo "\n-- Testing fgetss() with file opened using $file_modes[$mode_counter] mode --\n";
  40. /* create an empty file and write the strings with tags */
  41. $file_handle = fopen($filename, $file_modes[$mode_counter]);
  42. fwrite($file_handle,$string_with_tags); //writing data to the file
  43. if(!$file_handle) {
  44. echo "Error: failed to open file $filename!\n";
  45. exit();
  46. }
  47. // rewind the file pointer to beginning of the file
  48. var_dump( filesize($filename) );
  49. var_dump( rewind($file_handle) );
  50. var_dump( ftell($file_handle) );
  51. var_dump( feof($file_handle) );
  52. echo "-- Reading when file pointer points to EOF --\n";
  53. var_dump( fseek($file_handle,0,SEEK_END) ); // now file pointer at end
  54. var_dump( ftell($file_handle) ); //ensure file pointer at end
  55. var_dump( fgetss($file_handle) ); // try to read
  56. var_dump( ftell($file_handle) ); // find out file position
  57. var_dump( feof($file_handle) ); // ensure that file pointer is at eof
  58. // now file is at the end try reading with length and allowable tags,expecting false
  59. var_dump( fgetss($file_handle, 80, "<test>, <html>, <?>") );
  60. var_dump( ftell($file_handle) ); // find out file position
  61. var_dump( feof($file_handle) ); // ensure that file pointer is at eof
  62. // close the file
  63. fclose($file_handle);
  64. // delete the file
  65. unlink($filename);
  66. } // end of for - mode_counter
  67. echo "Done\n";
  68. ?>
  69. --EXPECT--
  70. *** Testing fgetss() : usage variations ***
  71. -- Testing fgetss() with file opened using w+ mode --
  72. int(445)
  73. bool(true)
  74. int(0)
  75. bool(false)
  76. -- Reading when file pointer points to EOF --
  77. int(0)
  78. int(445)
  79. bool(false)
  80. int(445)
  81. bool(true)
  82. bool(false)
  83. int(445)
  84. bool(true)
  85. -- Testing fgetss() with file opened using w+b mode --
  86. int(445)
  87. bool(true)
  88. int(0)
  89. bool(false)
  90. -- Reading when file pointer points to EOF --
  91. int(0)
  92. int(445)
  93. bool(false)
  94. int(445)
  95. bool(true)
  96. bool(false)
  97. int(445)
  98. bool(true)
  99. -- Testing fgetss() with file opened using w+t mode --
  100. int(453)
  101. bool(true)
  102. int(0)
  103. bool(false)
  104. -- Reading when file pointer points to EOF --
  105. int(0)
  106. int(453)
  107. bool(false)
  108. int(453)
  109. bool(true)
  110. bool(false)
  111. int(453)
  112. bool(true)
  113. -- Testing fgetss() with file opened using a+ mode --
  114. int(445)
  115. bool(true)
  116. int(0)
  117. bool(false)
  118. -- Reading when file pointer points to EOF --
  119. int(0)
  120. int(445)
  121. bool(false)
  122. int(445)
  123. bool(true)
  124. bool(false)
  125. int(445)
  126. bool(true)
  127. -- Testing fgetss() with file opened using a+b mode --
  128. int(445)
  129. bool(true)
  130. int(0)
  131. bool(false)
  132. -- Reading when file pointer points to EOF --
  133. int(0)
  134. int(445)
  135. bool(false)
  136. int(445)
  137. bool(true)
  138. bool(false)
  139. int(445)
  140. bool(true)
  141. -- Testing fgetss() with file opened using a+t mode --
  142. int(453)
  143. bool(true)
  144. int(0)
  145. bool(false)
  146. -- Reading when file pointer points to EOF --
  147. int(0)
  148. int(453)
  149. bool(false)
  150. int(453)
  151. bool(true)
  152. bool(false)
  153. int(453)
  154. bool(true)
  155. -- Testing fgetss() with file opened using x+ mode --
  156. int(445)
  157. bool(true)
  158. int(0)
  159. bool(false)
  160. -- Reading when file pointer points to EOF --
  161. int(0)
  162. int(445)
  163. bool(false)
  164. int(445)
  165. bool(true)
  166. bool(false)
  167. int(445)
  168. bool(true)
  169. -- Testing fgetss() with file opened using x+b mode --
  170. int(445)
  171. bool(true)
  172. int(0)
  173. bool(false)
  174. -- Reading when file pointer points to EOF --
  175. int(0)
  176. int(445)
  177. bool(false)
  178. int(445)
  179. bool(true)
  180. bool(false)
  181. int(445)
  182. bool(true)
  183. -- Testing fgetss() with file opened using x+t mode --
  184. int(453)
  185. bool(true)
  186. int(0)
  187. bool(false)
  188. -- Reading when file pointer points to EOF --
  189. int(0)
  190. int(453)
  191. bool(false)
  192. int(453)
  193. bool(true)
  194. bool(false)
  195. int(453)
  196. bool(true)
  197. Done