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