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