strstr.phpt 9.4 KB

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