fgetss_variation4.phpt 4.0 KB

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