"Fossies" - the Fresh Open Source Software Archive 
Member "php-8.0.28-src/tests/strings/001.phpt" (14 Feb 2023, 4933 Bytes) of package /windows/www/php-8.0.28-src.zip:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 --TEST--
2 String functions
3 --FILE--
4 <?php
5
6 echo "Testing strtok: ";
7
8 $str = "testing 1/2\\3";
9 $tok1 = strtok($str, " ");
10 $tok2 = strtok("/");
11 $tok3 = strtok("\\");
12 $tok4 = strtok(".");
13 if ($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
25 echo "Testing strstr: ";
26 $test = "This is a test";
27 $found1 = strstr($test, chr(32));
28 $found2 = strstr($test, "a ");
29 if ($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
37 echo "Testing strrchr: ";
38 $test = "fola fola blakken";
39 $found1 = strrchr($test, "b");
40 $found2 = strrchr($test, chr(102));
41 if ($found1 != "blakken") {
42 echo("failed 1\n");
43 } elseif ($found2 != "fola blakken") {
44 echo("failed 2\n");
45 }
46 else {
47 echo("passed\n");
48 }
49
50 echo "Testing strtoupper: ";
51 $test = "abCdEfg";
52 $upper = strtoupper($test);
53 if ($upper == "ABCDEFG") {
54 echo("passed\n");
55 } else {
56 echo("failed!\n");
57 }
58
59 echo "Testing strtolower: ";
60 $test = "ABcDeFG";
61 $lower = strtolower($test);
62 if ($lower == "abcdefg") {
63 echo("passed\n");
64 } else {
65 echo("failed!\n");
66 }
67
68 echo "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
81 if ($tests == $ok) {
82 echo("passed\n");
83 } else {
84 echo("failed!\n");
85 }
86
87 $raw = ' !"#$%&\'()*+,-./0123456789:;<=>?'
88 . '@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'
89 . '`abcdefghijklmnopqrstuvwxyz{|}~'
90 . "\0";
91
92 echo "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';
98 if ($encoded == $correct) {
99 echo("passed\n");
100 } else {
101 echo("failed!\n");
102 }
103
104 echo "Testing rawurldecode: ";
105 $decoded = rawurldecode($correct);
106 if ($decoded == $raw) {
107 echo("passed\n");
108 } else {
109 echo("failed!\n");
110 }
111
112 echo "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';
118 if ($encoded == $correct) {
119 echo("passed\n");
120 } else {
121 echo("failed!\n");
122 }
123
124 echo "Testing urldecode: ";
125 $decoded = urldecode($correct);
126 if ($decoded == $raw) {
127 echo("passed\n");
128 } else {
129 echo("failed!\n");
130 }
131
132 echo "Testing quotemeta: ";
133 $raw = "a.\\+*?" . chr(91) . "^" . chr(93) . "b\$c";
134 $quoted = quotemeta($raw);
135 if ($quoted == "a\\.\\\\\\+\\*\\?\\[\\^\\]b\\\$c") {
136 echo("passed\n");
137 } else {
138 echo("failed!\n");
139 }
140
141 echo "Testing ufirst: ";
142 $str = "fahrvergnuegen";
143 $uc = ucfirst($str);
144 if ($uc == "Fahrvergnuegen") {
145 echo("passed\n");
146 } else {
147 echo("failed!\n");
148 }
149
150 echo "Testing strtr: ";
151 $str = "test abcdefgh";
152 $tr = strtr($str, "def", "456");
153 if ($tr == "t5st abc456gh") {
154 echo("passed\n");
155 } else {
156 echo("failed!\n");
157 }
158
159 echo "Testing addslashes: ";
160 $str = "\"\\'";
161 $as = addslashes($str);
162 if ($as == "\\\"\\\\\\'") {
163 echo("passed\n");
164 } else {
165 echo("failed!\n");
166 }
167
168 echo "Testing stripslashes: ";
169 $str = "\$\\'";
170 $ss = stripslashes($str);
171 if ($ss == "\$'") {
172 echo("passed\n");
173 } else {
174 echo("failed!\n");
175 }
176
177
178 echo "Testing uniqid(true): ";
179 $str = "prefix";
180 $ui1 = uniqid($str, true);
181 $ui2 = uniqid($str, true);
182
183 $len = 29;
184
185 if (strlen($ui1) == strlen($ui2) && strlen($ui1) == $len && $ui1 != $ui2) {
186 echo("passed\n");
187 } else {
188 echo("failed!\n");
189 }
190
191 echo "Testing uniqid(false): ";
192 $str = "prefix";
193 $ui1 = uniqid($str);
194 usleep( 1 );
195 $ui2 = uniqid($str);
196
197 $len = strncasecmp(PHP_OS, 'CYGWIN', 6) ? 19 : 29;
198
199 if (strlen($ui1) == strlen($ui2) && strlen($ui1) == $len && $ui1 != $ui2) {
200 echo("passed\n");
201 } else {
202 echo("failed!\n");
203 }
204
205 ?>
206 --EXPECT--
207 Testing strtok: passed
208 Testing strstr: passed
209 Testing strrchr: passed
210 Testing strtoupper: passed
211 Testing strtolower: passed
212 Testing substr: passed
213 Testing rawurlencode: passed
214 Testing rawurldecode: passed
215 Testing urlencode: passed
216 Testing urldecode: passed
217 Testing quotemeta: passed
218 Testing ufirst: passed
219 Testing strtr: passed
220 Testing addslashes: passed
221 Testing stripslashes: passed
222 Testing uniqid(true): passed
223 Testing uniqid(false): passed