PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/facebook/hiphop-php
PHP | 172 lines | 133 code | 28 blank | 11 comment | 1 complexity | 96dd5b189957acd179084dd31fb733b2 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: string strstr ( string $haystack, string $needle );
  3. Description: Find first occurrence of a string
  4. and reurns the rest of the string from that string
  5. */
  6. echo "*** Testing basic functionality of strstr() ***\n";
  7. var_dump( strstr("test string", "test") );
  8. var_dump( strstr("test string", "string") );
  9. var_dump( strstr("test string", "strin") );
  10. var_dump( strstr("test string", "t s") );
  11. var_dump( strstr("test string", "g") );
  12. var_dump( md5(strstr("te".chr(0)."st", chr(0))) );
  13. var_dump( strstr("tEst", "test") );
  14. var_dump( strstr("teSt", "test") );
  15. var_dump( @strstr("", "") );
  16. var_dump( @strstr("a", "") );
  17. var_dump( @strstr("", "a") );
  18. echo "\n*** Testing strstr() with various needles ***";
  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 string starts with 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 string starts with "needle" in $string */
  56. for( $i = 0; $i < count($needles); $i++ ) {
  57. echo "\n-- Iteration $i --\n";
  58. var_dump( strstr($string, $needles[$i]) );
  59. }
  60. echo "\n*** Testing Miscelleneous input data ***\n";
  61. echo "-- Passing objects as string and needle --\n";
  62. /* we get "Catchable fatal error: saying Object of class needle could not be
  63. converted to string" by default when an object is passed instead of string:
  64. The error can be avoided by choosing the __toString magix method as follows: */
  65. class string
  66. {
  67. function __toString() {
  68. return "Hello, world";
  69. }
  70. }
  71. $obj_string = new string;
  72. class needle
  73. {
  74. function __toString() {
  75. return "world";
  76. }
  77. }
  78. $obj_needle = new needle;
  79. var_dump(strstr("$obj_string", "$obj_needle"));
  80. echo "\n-- passing an array as string and needle --\n";
  81. $needles = array("hello", "?world", "!$%**()%**[][[[&@#~!");
  82. var_dump( strstr($needles, $needles) ); // won't work
  83. var_dump( strstr("hello?world,!$%**()%**[][[[&@#~!", "$needles[1]") ); // works
  84. var_dump( strstr("hello?world,!$%**()%**[][[[&@#~!", "$needles[2]") ); // works
  85. echo "\n-- passing Resources as string and needle --\n";
  86. $resource1 = fopen(__FILE__, "r");
  87. $resource2 = opendir(".");
  88. var_dump( strstr($resource1, $resource1) );
  89. var_dump( strstr($resource1, $resource2) );
  90. echo "\n-- Posiibilities with null --\n";
  91. var_dump( strstr("", NULL) );
  92. var_dump( strstr(NULL, NULL) );
  93. var_dump( strstr("a", NULL) );
  94. var_dump( strstr("/x0", "0") ); // Hexadecimal NUL
  95. echo "\n-- A longer and heredoc string --\n";
  96. $string = <<<EOD
  97. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  98. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  99. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  100. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  101. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  102. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  103. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  104. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  105. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  106. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  107. EOD;
  108. var_dump( strstr($string, "abcd") );
  109. var_dump( strstr($string, "1234") );
  110. echo "\n-- A heredoc null string --\n";
  111. $str = <<<EOD
  112. EOD;
  113. var_dump( strstr($str, "\0") );
  114. var_dump( strstr($str, NULL) );
  115. var_dump( strstr($str, "0") );
  116. echo "\n-- simple and complex syntax strings --\n";
  117. $needle = 'world';
  118. /* Simple syntax */
  119. var_dump( strstr("Hello, world", "$needle") ); // works
  120. var_dump( strstr("Hello, world'S", "$needle'S") ); // works
  121. var_dump( strstr("Hello, worldS", "$needleS") ); // won't work
  122. /* String with curly braces, complex syntax */
  123. var_dump( strstr("Hello, worldS", "${needle}S") ); // works
  124. var_dump( strstr("Hello, worldS", "{$needle}S") ); // works
  125. echo "\n-- complex strings containing other than 7-bit chars --\n";
  126. $str = chr(0).chr(128).chr(129).chr(234).chr(235).chr(254).chr(255);
  127. echo "- Positions of some chars in the string '$str' are as follows -\n";
  128. echo chr(128)." => ";
  129. var_dump( strstr($str, chr(128)) );
  130. echo chr(255)." => ";
  131. var_dump( strstr($str, chr(255)) );
  132. echo chr(256)." => ";
  133. var_dump( strstr($str, chr(256)) );
  134. echo "\n*** Testing error conditions ***";
  135. var_dump( strstr($string, ""));
  136. var_dump( strstr() ); // zero argument
  137. var_dump( strstr("") ); // null argument
  138. var_dump( strstr($string) ); // without "needle"
  139. var_dump( strstr("a", "b", "c") ); // args > expected
  140. var_dump( strstr(NULL, "") );
  141. echo "\nDone";
  142. fclose($resource1);
  143. closedir($resource2);
  144. ?>