strpos.phpt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. --TEST--
  2. Test strpos() function
  3. --INI--
  4. precision=14
  5. --FILE--
  6. <?php
  7. echo "*** Testing basic functionality of strpos() ***\n";
  8. var_dump( strpos("test string", "test") );
  9. var_dump( strpos("test string", "string") );
  10. var_dump( strpos("test string", "strin") );
  11. var_dump( strpos("test string", "t s") );
  12. var_dump( strpos("test string", "g") );
  13. var_dump( strpos("te".chr(0)."st", chr(0)) );
  14. var_dump( strpos("tEst", "test") );
  15. var_dump( strpos("teSt", "test") );
  16. var_dump( strpos("", "") );
  17. var_dump( strpos("a", "") );
  18. var_dump( strpos("", "a") );
  19. var_dump( strpos("\\\\a", "\\a") );
  20. echo "\n*** Testing strpos() to find various needles and a long string ***\n";
  21. $string =
  22. "Hello world,012033 -3.3445 NULL TRUE FALSE\0 abcd\xxyz \x000 octal\n
  23. abcd$:Hello world";
  24. /* needles in an array to get the position of needle in $string */
  25. $needles = array(
  26. "Hello world",
  27. "WORLD",
  28. "\0",
  29. "\x00",
  30. "\x000",
  31. "abcd",
  32. "xyz",
  33. "octal",
  34. "-3",
  35. -3,
  36. "-3.344",
  37. -3.344,
  38. "NULL",
  39. "0",
  40. 0,
  41. TRUE,
  42. "TRUE",
  43. "1",
  44. 1,
  45. FALSE,
  46. "FALSE",
  47. " ",
  48. " ",
  49. 'b',
  50. '\n',
  51. "\n",
  52. "12",
  53. "12twelve",
  54. $string
  55. );
  56. /* loop through to get the "needle" position in $string */
  57. for( $i = 0; $i < count($needles); $i++ ) {
  58. echo "Position of '$needles[$i]' is => ";
  59. var_dump( strpos($string, $needles[$i]) );
  60. }
  61. echo "\n*** Testing strpos() with possible variations in offset ***\n";
  62. $offset_values = array (
  63. 1, // offset = 1
  64. "string", // offset as string, converts to zero
  65. "", // offset as string, converts to zero
  66. "0",
  67. TRUE,
  68. FALSE,
  69. "string12",
  70. -10, // Not found
  71. -15, // Found
  72. -strlen($string),
  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. try {
  78. var_dump( strpos($string, "Hello", $offset_values[$i]) );
  79. } catch (TypeError $e) {
  80. echo "\n", $e->getMessage(), "\n";
  81. }
  82. }
  83. echo "\n*** Testing miscellaneous input data ***\n";
  84. echo "-- Passing objects as string and needle --\n";
  85. /* we get "Recoverable fatal error: saying Object of class needle could not be
  86. converted to string" by default when an object is passed instead of string:
  87. The error can be avoided by choosing the __toString magix method as follows: */
  88. class StringCapable
  89. {
  90. function __toString() {
  91. return "Hello, world";
  92. }
  93. }
  94. $obj_string = new StringCapable;
  95. class needle
  96. {
  97. function __toString() {
  98. return "world";
  99. }
  100. }
  101. $obj_needle = new needle;
  102. var_dump( strpos("$obj_string", "$obj_needle") );
  103. echo "\n-- Posiibilities with null --\n";
  104. var_dump( strpos("", NULL) );
  105. var_dump( strpos(NULL, NULL) );
  106. var_dump( strpos("a", NULL) );
  107. var_dump( strpos("/x0", "0") ); // Hexadecimal NUL
  108. echo "\n-- A longer and heredoc string --\n";
  109. $string = <<<EOD
  110. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  111. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  112. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  113. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  114. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  115. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  116. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  117. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  118. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  119. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  120. EOD;
  121. var_dump( strpos($string, "abcd") );
  122. var_dump( strpos($string, "abcd", 72) ); // 72 -> "\n" in the first line
  123. var_dump( strpos($string, "abcd", 73) ); // 73 -> "abcd" in the second line
  124. var_dump( strpos($string, "9", (strlen($string)-1)) );
  125. echo "\n-- A heredoc null string --\n";
  126. $str = <<<EOD
  127. EOD;
  128. var_dump( strpos($str, "\0") );
  129. var_dump( strpos($str, "0") );
  130. echo "\n-- simple and complex syntax strings --\n";
  131. $needle = 'world';
  132. /* Simple syntax */
  133. var_dump( strpos("Hello, world", "$needle") ); // works
  134. var_dump( strpos("Hello, world'S", "$needle'S") ); // works
  135. var_dump( strpos("Hello, worldS", "$needleS") ); // won't work
  136. /* String with curly braces, complex syntax */
  137. var_dump( strpos("Hello, worldS", "${needle}S") ); // works
  138. var_dump( strpos("Hello, worldS", "{$needle}S") ); // works
  139. echo "\n*** Testing error conditions ***\n";
  140. var_dump( strpos($string, "") );
  141. try {
  142. strpos($string, "test", strlen($string)+1); // offset > strlen()
  143. } catch (ValueError $exception) {
  144. echo $exception->getMessage() . "\n";
  145. }
  146. try {
  147. strpos($string, "test", -strlen($string)-1); // offset before start
  148. } catch (ValueError $exception) {
  149. echo $exception->getMessage() . "\n";
  150. }
  151. ?>
  152. DONE
  153. --EXPECTF--
  154. *** Testing basic functionality of strpos() ***
  155. int(0)
  156. int(5)
  157. int(5)
  158. int(3)
  159. int(10)
  160. int(2)
  161. bool(false)
  162. bool(false)
  163. int(0)
  164. int(0)
  165. bool(false)
  166. int(1)
  167. *** Testing strpos() to find various needles and a long string ***
  168. Position of 'Hello world' is => int(0)
  169. Position of 'WORLD' is => bool(false)
  170. Position of '%0' is => int(46)
  171. Position of '%0' is => int(46)
  172. Position of '%00' is => int(58)
  173. Position of 'abcd' is => int(48)
  174. Position of 'xyz' is => int(54)
  175. Position of 'octal' is => int(61)
  176. Position of '-3' is => int(19)
  177. Position of '-3' is => int(19)
  178. Position of '-3.344' is => int(19)
  179. Position of '-3.344' is => int(19)
  180. Position of 'NULL' is => int(31)
  181. Position of '0' is => int(12)
  182. Position of '0' is => int(12)
  183. Position of '1' is => int(13)
  184. Position of 'TRUE' is => int(36)
  185. Position of '1' is => int(13)
  186. Position of '1' is => int(13)
  187. Position of '' is => int(0)
  188. Position of 'FALSE' is => int(41)
  189. Position of ' ' is => int(5)
  190. Position of ' ' is => int(26)
  191. Position of 'b' is => int(49)
  192. Position of '\n' is => bool(false)
  193. Position of '
  194. ' is => int(66)
  195. Position of '12' is => int(13)
  196. Position of '12twelve' is => bool(false)
  197. Position of 'Hello world,012033 -3.3445 NULL TRUE FALSE%0 abcd\xxyz %00 octal
  198. abcd$:Hello world' is => int(0)
  199. *** Testing strpos() with possible variations in offset ***
  200. Position of 'Hello' with offset '1' is => int(74)
  201. Position of 'Hello' with offset 'string' is =>
  202. strpos(): Argument #3 ($offset) must be of type int, string given
  203. Position of 'Hello' with offset '' is =>
  204. strpos(): Argument #3 ($offset) must be of type int, string given
  205. Position of 'Hello' with offset '0' is => int(0)
  206. Position of 'Hello' with offset '1' is => int(74)
  207. Position of 'Hello' with offset '' is => int(0)
  208. Position of 'Hello' with offset 'string12' is =>
  209. strpos(): Argument #3 ($offset) must be of type int, string given
  210. Position of 'Hello' with offset '-10' is => bool(false)
  211. Position of 'Hello' with offset '-15' is => int(74)
  212. Position of 'Hello' with offset '-85' is => int(0)
  213. *** Testing miscellaneous input data ***
  214. -- Passing objects as string and needle --
  215. int(7)
  216. -- Posiibilities with null --
  217. Deprecated: strpos(): Passing null to parameter #2 ($needle) of type string is deprecated in %s on line %d
  218. int(0)
  219. Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in %s on line %d
  220. Deprecated: strpos(): Passing null to parameter #2 ($needle) of type string is deprecated in %s on line %d
  221. int(0)
  222. Deprecated: strpos(): Passing null to parameter #2 ($needle) of type string is deprecated in %s on line %d
  223. int(0)
  224. int(2)
  225. -- A longer and heredoc string --
  226. int(0)
  227. int(73)
  228. int(73)
  229. int(728)
  230. -- A heredoc null string --
  231. bool(false)
  232. bool(false)
  233. -- simple and complex syntax strings --
  234. int(7)
  235. int(7)
  236. Warning: Undefined variable $needleS in %s on line %d
  237. int(0)
  238. int(7)
  239. int(7)
  240. *** Testing error conditions ***
  241. int(0)
  242. strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
  243. strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
  244. DONE