PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/2.0/Tests/@PHP/strings/001.php

#
PHP | 191 lines | 174 code | 17 blank | 0 comment | 62 complexity | 97853579a94494b8f82910008b1fc9d1 MD5 | raw file
Possible License(s): CPL-1.0, GPL-2.0, CC-BY-SA-3.0, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. [expect php]
  2. [file]
  3. <?php
  4. error_reporting(0);
  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, 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, 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%7E'
  87. . '%00';
  88. if (rawurldecode($encoded) == $raw) {
  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 (urldecode($encoded) == $raw) { // comparing the converted value does not work (casing, etc.)
  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: ";
  159. $str = "prefix";
  160. $ui1 = uniqid($str);
  161. $ui2 = uniqid($str);
  162. $len = strncasecmp(PHP_OS, 'CYGWIN', 6) ? 19 : 29;
  163. if (strlen($ui1) == strlen($ui2) && strlen($ui1) == $len && $ui1 != $ui2) {
  164. echo("passed\n");
  165. } else {
  166. echo("failed!\n");
  167. }
  168. ?>