001.phpt 4.8 KB

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