fgetss_variation4.phpt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. --TEST--
  2. Test fgetss() function : usage variations - read modes, file pointer at EOF
  3. --SKIPIF--
  4. <?php
  5. if(substr(PHP_OS, 0, 3) == "WIN")
  6. die("skip not for Windows");
  7. ?>
  8. --FILE--
  9. <?php
  10. error_reporting(E_ALL & ~E_DEPRECATED);
  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. // include the common file related test functions
  16. include ("file.inc");
  17. echo "*** Testing fgetss() : usage variations ***\n";
  18. /* string with html and php tags */
  19. $string_with_tags = <<<EOT
  20. <test>Testing fgetss() functions</test>
  21. <?php echo "this string is within php tag"; ?> {;}<{> this
  22. is a heredoc string. <pg>ksklnm@@$$&$&^%&^%&^%&</pg>
  23. <html> html </html> <?php echo "php"; ?>
  24. this line is without any html and php tags
  25. this is a line with more than eighty character,want to check line splitting correctly after 80 characters
  26. this is the text containing \r character
  27. this text contains some html tags <body> body </body> <br> br </br>
  28. this is the line with \n character.
  29. EOT;
  30. $filename = dirname(__FILE__)."/fgetss_variation4.tmp";
  31. /* try reading the file opened in different modes of reading */
  32. $file_modes = array("r","rb", "rt","r+", "r+b", "r+t");
  33. for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
  34. echo "\n-- Testing fgetss() with file opened using $file_modes[$mode_counter] mode --\n";
  35. /* create an empty file and write the strings with tags */
  36. create_file ($filename); //create an empty file
  37. file_put_contents($filename, $string_with_tags);
  38. $file_handle = fopen($filename, $file_modes[$mode_counter]);
  39. if(!$file_handle) {
  40. echo "Error: failed to open file $filename!\n";
  41. exit();
  42. }
  43. // rewind the file pointer to beginning of the file
  44. var_dump( filesize($filename) );
  45. var_dump( rewind($file_handle) );
  46. var_dump( ftell($file_handle) );
  47. var_dump( feof($file_handle) );
  48. echo "-- Reading when file pointer points to EOF --\n";
  49. var_dump( fseek($file_handle,0,SEEK_END) ); // now file pointer at end
  50. var_dump( ftell($file_handle) ); //ensure file pointer at end
  51. var_dump( fgetss($file_handle) ); // try to read
  52. var_dump( ftell($file_handle) ); // find out file position
  53. var_dump( feof($file_handle) ); // ensure that file pointer is at eof
  54. // now file is at the end try reading with length and allowable tags,expecting false
  55. var_dump( fgetss($file_handle, 80, "<test>, <html>, <?>") );
  56. var_dump( ftell($file_handle) ); // find out file position
  57. var_dump( feof($file_handle) ); // ensure that file pointer is at eof
  58. // close the file
  59. fclose($file_handle);
  60. // delete the file
  61. delete_file($filename);
  62. } // end of for - mode_counter
  63. echo "Done\n";
  64. ?>
  65. --EXPECT--
  66. *** Testing fgetss() : usage variations ***
  67. -- Testing fgetss() with file opened using r mode --
  68. int(486)
  69. bool(true)
  70. int(0)
  71. bool(false)
  72. -- Reading when file pointer points to EOF --
  73. int(0)
  74. int(486)
  75. bool(false)
  76. int(486)
  77. bool(true)
  78. bool(false)
  79. int(486)
  80. bool(true)
  81. -- Testing fgetss() with file opened using rb mode --
  82. int(486)
  83. bool(true)
  84. int(0)
  85. bool(false)
  86. -- Reading when file pointer points to EOF --
  87. int(0)
  88. int(486)
  89. bool(false)
  90. int(486)
  91. bool(true)
  92. bool(false)
  93. int(486)
  94. bool(true)
  95. -- Testing fgetss() with file opened using rt mode --
  96. int(486)
  97. bool(true)
  98. int(0)
  99. bool(false)
  100. -- Reading when file pointer points to EOF --
  101. int(0)
  102. int(486)
  103. bool(false)
  104. int(486)
  105. bool(true)
  106. bool(false)
  107. int(486)
  108. bool(true)
  109. -- Testing fgetss() with file opened using r+ mode --
  110. int(486)
  111. bool(true)
  112. int(0)
  113. bool(false)
  114. -- Reading when file pointer points to EOF --
  115. int(0)
  116. int(486)
  117. bool(false)
  118. int(486)
  119. bool(true)
  120. bool(false)
  121. int(486)
  122. bool(true)
  123. -- Testing fgetss() with file opened using r+b mode --
  124. int(486)
  125. bool(true)
  126. int(0)
  127. bool(false)
  128. -- Reading when file pointer points to EOF --
  129. int(0)
  130. int(486)
  131. bool(false)
  132. int(486)
  133. bool(true)
  134. bool(false)
  135. int(486)
  136. bool(true)
  137. -- Testing fgetss() with file opened using r+t mode --
  138. int(486)
  139. bool(true)
  140. int(0)
  141. bool(false)
  142. -- Reading when file pointer points to EOF --
  143. int(0)
  144. int(486)
  145. bool(false)
  146. int(486)
  147. bool(true)
  148. bool(false)
  149. int(486)
  150. bool(true)
  151. Done