001.phpt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. --TEST--
  2. String functions
  3. --FILE--
  4. <?php
  5. error_reporting(0);
  6. echo "Testing strtok: ";
  7. $str = "testing 1/2\\3";
  8. $tok1 = strtok($str, " ");
  9. $tok2 = strtok("/");
  10. $tok3 = strtok("\\");
  11. $tok4 = strtok(".");
  12. if ($tok1 != "testing") {
  13. echo("failed 1\n");
  14. } elseif ($tok2 != "1") {
  15. echo("failed 2\n");
  16. } elseif ($tok3 != "2") {
  17. echo("failed 3\n");
  18. } elseif ($tok4 != "3") {
  19. echo("failed 4\n");
  20. } else {
  21. echo("passed\n");
  22. }
  23. echo "Testing strstr: ";
  24. $test = "This is a test";
  25. $found1 = strstr($test, 32);
  26. $found2 = strstr($test, "a ");
  27. if ($found1 != " is a test") {
  28. echo("failed 1\n");
  29. } elseif ($found2 != "a test") {
  30. echo("failed 2\n");
  31. } else {
  32. echo("passed\n");
  33. }
  34. echo "Testing strrchr: ";
  35. $test = "fola fola blakken";
  36. $found1 = strrchr($test, "b");
  37. $found2 = strrchr($test, 102);
  38. if ($found1 != "blakken") {
  39. echo("failed 1\n");
  40. } elseif ($found2 != "fola blakken") {
  41. echo("failed 2\n");
  42. }
  43. else {
  44. echo("passed\n");
  45. }
  46. echo "Testing strtoupper: ";
  47. $test = "abCdEfg";
  48. $upper = strtoupper($test);
  49. if ($upper == "ABCDEFG") {
  50. echo("passed\n");
  51. } else {
  52. echo("failed!\n");
  53. }
  54. echo "Testing strtolower: ";
  55. $test = "ABcDeFG";
  56. $lower = strtolower($test);
  57. if ($lower == "abcdefg") {
  58. echo("passed\n");
  59. } else {
  60. echo("failed!\n");
  61. }
  62. echo "Testing substr: ";
  63. $tests = $ok = 0;
  64. $string = "string12345";
  65. $tests++; if (substr($string, 2, 10) == "ring12345") { $ok++; }
  66. $tests++; if (substr($string, 4, 7) == "ng12345") { $ok++; }
  67. $tests++; if (substr($string, 4) == "ng12345") { $ok++; }
  68. $tests++; if (substr($string, 10, 2) == "5") { $ok++; }
  69. $tests++; if (substr($string, 6, 0) == "") { $ok++; }
  70. $tests++; if (substr($string, -2, 2) == "45") { $ok++; }
  71. $tests++; if (substr($string, 1, -1) == "tring1234") { $ok++; }
  72. $tests++; if (substr($string, -1, -2) == "") { $ok++; }
  73. $tests++; if (substr($string, -3, -2) == "3") { $ok++; }
  74. if ($tests == $ok) {
  75. echo("passed\n");
  76. } else {
  77. echo("failed!\n");
  78. }
  79. $raw = ' !"#$%&\'()*+,-./0123456789:;<=>?'
  80. . '@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'
  81. . '`abcdefghijklmnopqrstuvwxyz{|}~'
  82. . "\0";
  83. echo "Testing rawurlencode: ";
  84. $encoded = rawurlencode($raw);
  85. $correct = '%20%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F'
  86. . '%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_'
  87. . '%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~'
  88. . '%00';
  89. if ($encoded == $correct) {
  90. echo("passed\n");
  91. } else {
  92. echo("failed!\n");
  93. }
  94. echo "Testing rawurldecode: ";
  95. $decoded = rawurldecode($correct);
  96. if ($decoded == $raw) {
  97. echo("passed\n");
  98. } else {
  99. echo("failed!\n");
  100. }
  101. echo "Testing urlencode: ";
  102. $encoded = urlencode($raw);
  103. $correct = '+%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F'
  104. . '%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_'
  105. . '%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D%7E'
  106. . '%00';
  107. if ($encoded == $correct) {
  108. echo("passed\n");
  109. } else {
  110. echo("failed!\n");
  111. }
  112. echo "Testing urldecode: ";
  113. $decoded = urldecode($correct);
  114. if ($decoded == $raw) {
  115. echo("passed\n");
  116. } else {
  117. echo("failed!\n");
  118. }
  119. echo "Testing quotemeta: ";
  120. $raw = "a.\\+*?" . chr(91) . "^" . chr(93) . "b\$c";
  121. $quoted = quotemeta($raw);
  122. if ($quoted == "a\\.\\\\\\+\\*\\?\\[\\^\\]b\\\$c") {
  123. echo("passed\n");
  124. } else {
  125. echo("failed!\n");
  126. }
  127. echo "Testing ufirst: ";
  128. $str = "fahrvergnuegen";
  129. $uc = ucfirst($str);
  130. if ($uc == "Fahrvergnuegen") {
  131. echo("passed\n");
  132. } else {
  133. echo("failed!\n");
  134. }
  135. echo "Testing strtr: ";
  136. $str = "test abcdefgh";
  137. $tr = strtr($str, "def", "456");
  138. if ($tr == "t5st abc456gh") {
  139. echo("passed\n");
  140. } else {
  141. echo("failed!\n");
  142. }
  143. echo "Testing addslashes: ";
  144. $str = "\"\\'";
  145. $as = addslashes($str);
  146. if ($as == "\\\"\\\\\\'") {
  147. echo("passed\n");
  148. } else {
  149. echo("failed!\n");
  150. }
  151. echo "Testing stripslashes: ";
  152. $str = "\$\\'";
  153. $ss = stripslashes($str);
  154. if ($ss == "\$'") {
  155. echo("passed\n");
  156. } else {
  157. echo("failed!\n");
  158. }
  159. echo "Testing uniqid(true): ";
  160. $str = "prefix";
  161. $ui1 = uniqid($str, true);
  162. $ui2 = uniqid($str, true);
  163. $len = 29;
  164. if (strlen($ui1) == strlen($ui2) && strlen($ui1) == $len && $ui1 != $ui2) {
  165. echo("passed\n");
  166. } else {
  167. echo("failed!\n");
  168. }
  169. echo "Testing uniqid(false): ";
  170. $str = "prefix";
  171. $ui1 = uniqid($str);
  172. usleep( 1 );
  173. $ui2 = uniqid($str);
  174. $len = strncasecmp(PHP_OS, 'CYGWIN', 6) ? 19 : 29;
  175. if (strlen($ui1) == strlen($ui2) && strlen($ui1) == $len && $ui1 != $ui2) {
  176. echo("passed\n");
  177. } else {
  178. echo("failed!\n");
  179. }
  180. ?>
  181. --EXPECT--
  182. Testing strtok: passed
  183. Testing strstr: passed
  184. Testing strrchr: passed
  185. Testing strtoupper: passed
  186. Testing strtolower: passed
  187. Testing substr: passed
  188. Testing rawurlencode: passed
  189. Testing rawurldecode: passed
  190. Testing urlencode: passed
  191. Testing urldecode: passed
  192. Testing quotemeta: passed
  193. Testing ufirst: passed
  194. Testing strtr: passed
  195. Testing addslashes: passed
  196. Testing stripslashes: passed
  197. Testing uniqid(true): passed
  198. Testing uniqid(false): passed