1--TEST--
2Test explode() function
3--INI--
4error_reporting=2047
5precision=14
6--FILE--
7<?php
8/* Prototype: array explode ( string $delimiter, string $string [, int $limit] );
9   Description: Returns an array of strings, each of which is a substring of string
10                formed by splitting it on boundaries formed by the string delimiter.
11                If limit is set, the returned array will contain a maximum of limit
12                elements with the last element containing the rest of string.
13*/
14
15echo "*** Testing explode() for basic operations ***\n";
16$delimiters = array (
17  "",  // len=0
18  NULL,
19  "abcd",  // string
20  0,  // zero
21  "0",
22  TRUE,  // boolean value
23  FALSE,
24  -1,  // negative integer
25  -11.23,  // double
26  4,  // positive integer
27  "%",
28);
29$string = "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND";
30/* loop prints an array of strings, each of which is a substring of $string
31   formed by splitting it on boundaries formed by the string $delimiter.
32 */
33$counter = 1;
34foreach($delimiters as $delimiter) {
35  echo "-- Iteration $counter --\n";
36  var_dump( explode($delimiter, $string, -1) );
37  var_dump( explode($delimiter, $string, 0) );
38  var_dump( explode($delimiter, $string, 1) );
39  var_dump( explode($delimiter, $string, 2) );
40  $counter++;
41}
42
43echo "\n*** Testing explode() with miscelleneous input arguments ***\n";
44
45echo "\n-- Passing positive values of Limit to explode() --\n";
46/* LIMIT=2 */
47var_dump( explode("::", "mon::tues::wed::thurs::fri::sat::sun", 2) );
48
49/* checking for LIMIT =0,1 */
50echo "\n-- Passing limit values 0 and 1 to explode() --\n";
51var_dump( explode(":", "Name:Phone:Address:City:State", 0) );
52var_dump( explode(":", "Name:Phone:Address:City:State", 1) );
53
54/* to check the maximum limit of string that can be given with limit<=0,
55   default size is 50 but increases dynamically */
56echo "\n*** Testing explode() for maximum limit of string with Limit = -1 ***\n";
57var_dump( explode(":", "1:2:3:4:5:6:7:7:5:6:7:3:4:5:2:8:9:0:5:5:5:5:5:5:5:5:5:5:5:5:55:5:5:5%:%:%:%:5:5:5:%:%:5:5:5:5:5%:%:%:55:1:1", -1) );
58
59echo "\n*** Testing explode() with string variations as input argument ***\n";
60/* String with escape characters */
61echo "\n-- Testing string with escape characters --\n";
62var_dump( explode("\t\n", "1234\t\n5678\n\t9100") );
63var_dump( explode("\r", "1234\rabcd\r5678\rrstu") );
64
65/* String with embedded NULL */
66echo "\n-- Testing string with embedded NULL --\n";
67var_dump( explode("\x00", "abcd\x0n1234\x0005678\x0000efgh\xijkl") );
68var_dump( explode("\0", "abcd\0efgh\0ijkl\x00mnop\x000qrst\00uvwx\000yz") );
69
70/* Checking OBJECTS type */
71echo "\n*** Testing explode() with objects ***\n";
72class string1 {
73  public function __toString() {
74    return "Object";
75  }
76}
77$obj = new string1;
78var_dump( explode("b", $obj) );
79
80echo "\n*** Testing error conditions ***\n";
81/* checking for arguments <2 and >3 */
82var_dump( explode(":", "array1:array2:array3", -1, -33) );
83var_dump( explode(":") );
84
85echo "Done\n";
86?>
87--EXPECTF--
88*** Testing explode() for basic operations ***
89-- Iteration 1 --
90
91Warning: explode(): Empty delimiter in %s on line %d
92bool(false)
93
94Warning: explode(): Empty delimiter in %s on line %d
95bool(false)
96
97Warning: explode(): Empty delimiter in %s on line %d
98bool(false)
99
100Warning: explode(): Empty delimiter in %s on line %d
101bool(false)
102-- Iteration 2 --
103
104Warning: explode(): Empty delimiter in %s on line %d
105bool(false)
106
107Warning: explode(): Empty delimiter in %s on line %d
108bool(false)
109
110Warning: explode(): Empty delimiter in %s on line %d
111bool(false)
112
113Warning: explode(): Empty delimiter in %s on line %d
114bool(false)
115-- Iteration 3 --
116array(1) {
117  [0]=>
118  string(10) "1234NULL23"
119}
120array(1) {
121  [0]=>
122  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
123}
124array(1) {
125  [0]=>
126  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
127}
128array(2) {
129  [0]=>
130  string(10) "1234NULL23"
131  [1]=>
132  string(43) "00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
133}
134-- Iteration 4 --
135array(5) {
136  [0]=>
137  string(14) "1234NULL23abcd"
138  [1]=>
139  string(0) ""
140  [2]=>
141  string(0) ""
142  [3]=>
143  string(0) ""
144  [4]=>
145  string(0) ""
146}
147array(1) {
148  [0]=>
149  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
150}
151array(1) {
152  [0]=>
153  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
154}
155array(2) {
156  [0]=>
157  string(14) "1234NULL23abcd"
158  [1]=>
159  string(42) "0000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
160}
161-- Iteration 5 --
162array(5) {
163  [0]=>
164  string(14) "1234NULL23abcd"
165  [1]=>
166  string(0) ""
167  [2]=>
168  string(0) ""
169  [3]=>
170  string(0) ""
171  [4]=>
172  string(0) ""
173}
174array(1) {
175  [0]=>
176  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
177}
178array(1) {
179  [0]=>
180  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
181}
182array(2) {
183  [0]=>
184  string(14) "1234NULL23abcd"
185  [1]=>
186  string(42) "0000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
187}
188-- Iteration 6 --
189array(5) {
190  [0]=>
191  string(0) ""
192  [1]=>
193  string(28) "234NULL23abcd00000TRUEFALSE-"
194  [2]=>
195  string(0) ""
196  [3]=>
197  string(12) ".234444true-"
198  [4]=>
199  string(0) ""
200}
201array(1) {
202  [0]=>
203  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
204}
205array(1) {
206  [0]=>
207  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
208}
209array(2) {
210  [0]=>
211  string(0) ""
212  [1]=>
213  string(56) "234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
214}
215-- Iteration 7 --
216
217Warning: explode(): Empty delimiter in %s on line %d
218bool(false)
219
220Warning: explode(): Empty delimiter in %s on line %d
221bool(false)
222
223Warning: explode(): Empty delimiter in %s on line %d
224bool(false)
225
226Warning: explode(): Empty delimiter in %s on line %d
227bool(false)
228-- Iteration 8 --
229array(2) {
230  [0]=>
231  string(28) "1234NULL23abcd00000TRUEFALSE"
232  [1]=>
233  string(12) "1.234444true"
234}
235array(1) {
236  [0]=>
237  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
238}
239array(1) {
240  [0]=>
241  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
242}
243array(2) {
244  [0]=>
245  string(28) "1234NULL23abcd00000TRUEFALSE"
246  [1]=>
247  string(27) "1.234444true-11.24%PHP%ZEND"
248}
249-- Iteration 9 --
250array(1) {
251  [0]=>
252  string(28) "1234NULL23abcd00000TRUEFALSE"
253}
254array(1) {
255  [0]=>
256  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
257}
258array(1) {
259  [0]=>
260  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
261}
262array(2) {
263  [0]=>
264  string(28) "1234NULL23abcd00000TRUEFALSE"
265  [1]=>
266  string(23) "4444true-11.24%PHP%ZEND"
267}
268-- Iteration 10 --
269array(6) {
270  [0]=>
271  string(3) "123"
272  [1]=>
273  string(30) "NULL23abcd00000TRUEFALSE-11.23"
274  [2]=>
275  string(0) ""
276  [3]=>
277  string(0) ""
278  [4]=>
279  string(0) ""
280  [5]=>
281  string(9) "true-11.2"
282}
283array(1) {
284  [0]=>
285  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
286}
287array(1) {
288  [0]=>
289  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
290}
291array(2) {
292  [0]=>
293  string(3) "123"
294  [1]=>
295  string(53) "NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
296}
297-- Iteration 11 --
298array(2) {
299  [0]=>
300  string(48) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24"
301  [1]=>
302  string(3) "PHP"
303}
304array(1) {
305  [0]=>
306  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
307}
308array(1) {
309  [0]=>
310  string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
311}
312array(2) {
313  [0]=>
314  string(48) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24"
315  [1]=>
316  string(8) "PHP%ZEND"
317}
318
319*** Testing explode() with miscelleneous input arguments ***
320
321-- Passing positive values of Limit to explode() --
322array(2) {
323  [0]=>
324  string(3) "mon"
325  [1]=>
326  string(31) "tues::wed::thurs::fri::sat::sun"
327}
328
329-- Passing limit values 0 and 1 to explode() --
330array(1) {
331  [0]=>
332  string(29) "Name:Phone:Address:City:State"
333}
334array(1) {
335  [0]=>
336  string(29) "Name:Phone:Address:City:State"
337}
338
339*** Testing explode() for maximum limit of string with Limit = -1 ***
340array(51) {
341  [0]=>
342  string(1) "1"
343  [1]=>
344  string(1) "2"
345  [2]=>
346  string(1) "3"
347  [3]=>
348  string(1) "4"
349  [4]=>
350  string(1) "5"
351  [5]=>
352  string(1) "6"
353  [6]=>
354  string(1) "7"
355  [7]=>
356  string(1) "7"
357  [8]=>
358  string(1) "5"
359  [9]=>
360  string(1) "6"
361  [10]=>
362  string(1) "7"
363  [11]=>
364  string(1) "3"
365  [12]=>
366  string(1) "4"
367  [13]=>
368  string(1) "5"
369  [14]=>
370  string(1) "2"
371  [15]=>
372  string(1) "8"
373  [16]=>
374  string(1) "9"
375  [17]=>
376  string(1) "0"
377  [18]=>
378  string(1) "5"
379  [19]=>
380  string(1) "5"
381  [20]=>
382  string(1) "5"
383  [21]=>
384  string(1) "5"
385  [22]=>
386  string(1) "5"
387  [23]=>
388  string(1) "5"
389  [24]=>
390  string(1) "5"
391  [25]=>
392  string(1) "5"
393  [26]=>
394  string(1) "5"
395  [27]=>
396  string(1) "5"
397  [28]=>
398  string(1) "5"
399  [29]=>
400  string(1) "5"
401  [30]=>
402  string(2) "55"
403  [31]=>
404  string(1) "5"
405  [32]=>
406  string(1) "5"
407  [33]=>
408  string(2) "5%"
409  [34]=>
410  string(1) "%"
411  [35]=>
412  string(1) "%"
413  [36]=>
414  string(1) "%"
415  [37]=>
416  string(1) "5"
417  [38]=>
418  string(1) "5"
419  [39]=>
420  string(1) "5"
421  [40]=>
422  string(1) "%"
423  [41]=>
424  string(1) "%"
425  [42]=>
426  string(1) "5"
427  [43]=>
428  string(1) "5"
429  [44]=>
430  string(1) "5"
431  [45]=>
432  string(1) "5"
433  [46]=>
434  string(2) "5%"
435  [47]=>
436  string(1) "%"
437  [48]=>
438  string(1) "%"
439  [49]=>
440  string(2) "55"
441  [50]=>
442  string(1) "1"
443}
444
445*** Testing explode() with string variations as input argument ***
446
447-- Testing string with escape characters --
448array(2) {
449  [0]=>
450  string(4) "1234"
451  [1]=>
452  string(10) "5678
453	9100"
454}
455array(4) {
456  [0]=>
457  string(4) "1234"
458  [1]=>
459  string(4) "abcd"
460  [2]=>
461  string(4) "5678"
462  [3]=>
463  string(4) "rstu"
464}
465
466-- Testing string with embedded NULL --
467array(4) {
468  [0]=>
469  string(4) "abcd"
470  [1]=>
471  string(5) "n1234"
472  [2]=>
473  string(5) "05678"
474  [3]=>
475  string(12) "00efgh\xijkl"
476}
477array(7) {
478  [0]=>
479  string(4) "abcd"
480  [1]=>
481  string(4) "efgh"
482  [2]=>
483  string(4) "ijkl"
484  [3]=>
485  string(4) "mnop"
486  [4]=>
487  string(5) "0qrst"
488  [5]=>
489  string(4) "uvwx"
490  [6]=>
491  string(2) "yz"
492}
493
494*** Testing explode() with objects ***
495array(2) {
496  [0]=>
497  string(1) "O"
498  [1]=>
499  string(4) "ject"
500}
501
502*** Testing error conditions ***
503
504Warning: explode() expects at most 3 parameters, 4 given in %s on line %d
505NULL
506
507Warning: explode() expects at least 2 parameters, 1 given in %s on line %d
508NULL
509Done
510