PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/test/zend/bad/ext/standard/tests/strings/strpos.php

http://github.com/facebook/hiphop-php
PHP | 196 lines | 156 code | 29 blank | 11 comment | 2 complexity | b397885be6d68f52bb39cb925ff18bcc MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?php
  2. /* Prototype: int strpos ( string $haystack, mixed $needle [, int $offset] );
  3. Description: Find position of first occurrence of a string
  4. */
  5. echo "*** Testing basic functionality of strpos() ***\n";
  6. var_dump( strpos("test string", "test") );
  7. var_dump( strpos("test string", "string") );
  8. var_dump( strpos("test string", "strin") );
  9. var_dump( strpos("test string", "t s") );
  10. var_dump( strpos("test string", "g") );
  11. var_dump( strpos("te".chr(0)."st", chr(0)) );
  12. var_dump( strpos("tEst", "test") );
  13. var_dump( strpos("teSt", "test") );
  14. var_dump( @strpos("", "") );
  15. var_dump( @strpos("a", "") );
  16. var_dump( @strpos("", "a") );
  17. var_dump( @strpos("\\\\a", "\\a") );
  18. echo "\n*** Testing strpos() to find various needles and a long string ***\n";
  19. $string =
  20. "Hello world,012033 -3.3445 NULL TRUE FALSE\0 abcd\xxyz \x000 octal\n
  21. abcd$:Hello world";
  22. /* needles in an array to get the position of needle in $string */
  23. $needles = array(
  24. "Hello world",
  25. "WORLD",
  26. "\0",
  27. "\x00",
  28. "\x000",
  29. "abcd",
  30. "xyz",
  31. "octal",
  32. "-3",
  33. -3,
  34. "-3.344",
  35. -3.344,
  36. NULL,
  37. "NULL",
  38. "0",
  39. 0,
  40. TRUE,
  41. "TRUE",
  42. "1",
  43. 1,
  44. FALSE,
  45. "FALSE",
  46. " ",
  47. " ",
  48. 'b',
  49. '\n',
  50. "\n",
  51. "12",
  52. "12twelve",
  53. $string
  54. );
  55. /* loop through to get the "needle" position in $string */
  56. for( $i = 0; $i < count($needles); $i++ ) {
  57. echo "Position of '$needles[$i]' is => ";
  58. var_dump( strpos($string, $needles[$i]) );
  59. }
  60. echo "\n*** Testing strpos() with possible variations in offset ***\n";
  61. $offset_values = array (
  62. 1, // offset = 1
  63. "string", // offset as string, converts to zero
  64. NULL, // offset as string, converts to zero
  65. "", // offset as string, converts to zero
  66. "12string", // mixed string with int and chars
  67. "0",
  68. TRUE,
  69. NULL,
  70. FALSE,
  71. "string12",
  72. "12.3string", // mixed string with float and chars
  73. );
  74. /* loop through to get the "needle" position in $string */
  75. for( $i = 0; $i < count( $offset_values ); $i++ ) {
  76. echo "Position of 'Hello' with offset '$offset_values[$i]' is => ";
  77. var_dump( strpos($string, "Hello", $offset_values[$i]) );
  78. }
  79. echo "\n*** Testing Miscelleneous input data ***\n";
  80. echo "-- Passing objects as string and needle --\n";
  81. /* we get "Catchable fatal error: saying Object of class needle could not be
  82. converted to string" by default when an object is passed instead of string:
  83. The error can be avoided by choosing the __toString magix method as follows: */
  84. class string
  85. {
  86. function __toString() {
  87. return "Hello, world";
  88. }
  89. }
  90. $obj_string = new string;
  91. class needle
  92. {
  93. function __toString() {
  94. return "world";
  95. }
  96. }
  97. $obj_needle = new needle;
  98. var_dump( strpos("$obj_string", "$obj_needle") );
  99. echo "\n-- Passing an array as string and needle --\n";
  100. $needles = array("hello", "?world", "!$%**()%**[][[[&@#~!");
  101. var_dump( strpos($needles, $needles) ); // won't work
  102. var_dump( strpos("hello?world,!$%**()%**[][[[&@#~!", "$needles[1]") ); // works
  103. var_dump( strpos("hello?world,!$%**()%**[][[[&@#~!", "$needles[2]") ); // works
  104. echo "\n-- Passing Resources as string and needle --\n";
  105. $resource1 = fopen(__FILE__, "r");
  106. $resource2 = opendir(".");
  107. var_dump( strpos($resource1, $resource1) );
  108. var_dump( strpos($resource1, $resource2) );
  109. echo "\n-- Posiibilities with null --\n";
  110. var_dump( strpos("", NULL) );
  111. var_dump( strpos(NULL, NULL) );
  112. var_dump( strpos("a", NULL) );
  113. var_dump( strpos("/x0", "0") ); // Hexadecimal NUL
  114. echo "\n-- A longer and heredoc string --\n";
  115. $string = <<<EOD
  116. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  117. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  118. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  119. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  120. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  121. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  122. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  123. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  124. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  125. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  126. EOD;
  127. var_dump( strpos($string, "abcd") );
  128. var_dump( strpos($string, "abcd", 72) ); // 72 -> "\n" in the first line
  129. var_dump( strpos($string, "abcd", 73) ); // 73 -> "abcd" in the second line
  130. var_dump( strpos($string, "9", (strlen($string)-1)) );
  131. echo "\n-- A heredoc null string --\n";
  132. $str = <<<EOD
  133. EOD;
  134. var_dump( strpos($str, "\0") );
  135. var_dump( strpos($str, NULL) );
  136. var_dump( strpos($str, "0") );
  137. echo "\n-- simple and complex syntax strings --\n";
  138. $needle = 'world';
  139. /* Simple syntax */
  140. var_dump( strpos("Hello, world", "$needle") ); // works
  141. var_dump( strpos("Hello, world'S", "$needle'S") ); // works
  142. var_dump( strpos("Hello, worldS", "$needleS") ); // won't work
  143. /* String with curly braces, complex syntax */
  144. var_dump( strpos("Hello, worldS", "${needle}S") ); // works
  145. var_dump( strpos("Hello, worldS", "{$needle}S") ); // works
  146. echo "\n-- complex strings containing other than 7-bit chars --\n";
  147. $str = chr(0).chr(128).chr(129).chr(234).chr(235).chr(254).chr(255);
  148. echo "-- Positions of some chars in the string '$str' are as follows --\n";
  149. echo chr(128)." => ";
  150. var_dump( strpos($str, chr(128)) );
  151. echo chr(255)." => ";
  152. var_dump( strpos($str, chr(255), 3) );
  153. echo chr(256)." => ";
  154. var_dump( strpos($str, chr(256)) );
  155. echo "\n*** Testing error conditions ***";
  156. var_dump( strpos($string, "") );
  157. var_dump( strpos() ); // zero argument
  158. var_dump( strpos("") ); // null argument
  159. var_dump( strpos($string) ); // without "needle"
  160. var_dump( strpos("a", "b", "c", "d") ); // args > expected
  161. var_dump( strpos($string, "test", strlen($string)+1) ); // offset > strlen()
  162. var_dump( strpos($string, "test", -1) ); // offset < 0
  163. var_dump( strpos(NULL, "") );
  164. echo "\nDone";
  165. fclose($resource1);
  166. closedir($resource2);
  167. ?>