strstr.phpt 10 KB

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