Annotation of embedaddon/php/ext/standard/tests/strings/explode1.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: Test explode() function
3: --INI--
4: error_reporting=2047
5: precision=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:
15: echo "*** 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;
34: foreach($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:
43: echo "\n*** Testing explode() with miscelleneous input arguments ***\n";
44:
45: echo "\n-- Passing positive values of Limit to explode() --\n";
46: /* LIMIT=2 */
47: var_dump( explode("::", "mon::tues::wed::thurs::fri::sat::sun", 2) );
48:
49: /* checking for LIMIT =0,1 */
50: echo "\n-- Passing limit values 0 and 1 to explode() --\n";
51: var_dump( explode(":", "Name:Phone:Address:City:State", 0) );
52: var_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 */
56: echo "\n*** Testing explode() for maximum limit of string with Limit = -1 ***\n";
57: var_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:
59: echo "\n*** Testing explode() with string variations as input argument ***\n";
60: /* String with escape characters */
61: echo "\n-- Testing string with escape characters --\n";
62: var_dump( explode("\t\n", "1234\t\n5678\n\t9100") );
63: var_dump( explode("\r", "1234\rabcd\r5678\rrstu") );
64:
65: /* String with embedded NULL */
66: echo "\n-- Testing string with embedded NULL --\n";
67: var_dump( explode("\x00", "abcd\x0n1234\x0005678\x0000efgh\xijkl") );
68: var_dump( explode("\0", "abcd\0efgh\0ijkl\x00mnop\x000qrst\00uvwx\000yz") );
69:
70: /* Checking OBJECTS type */
71: echo "\n*** Testing explode() with objects ***\n";
72: class string1 {
73: public function __toString() {
74: return "Object";
75: }
76: }
77: $obj = new string1;
78: var_dump( explode("b", $obj) );
79:
80: echo "\n*** Testing error conditions ***\n";
81: /* checking for arguments <2 and >3 */
82: var_dump( explode(":", "array1:array2:array3", -1, -33) );
83: var_dump( explode(":") );
84:
85: echo "Done\n";
86: ?>
87: --EXPECTF--
88: *** Testing explode() for basic operations ***
89: -- Iteration 1 --
90:
91: Warning: explode(): Empty delimiter in %s on line %d
92: bool(false)
93:
94: Warning: explode(): Empty delimiter in %s on line %d
95: bool(false)
96:
97: Warning: explode(): Empty delimiter in %s on line %d
98: bool(false)
99:
100: Warning: explode(): Empty delimiter in %s on line %d
101: bool(false)
102: -- Iteration 2 --
103:
104: Warning: explode(): Empty delimiter in %s on line %d
105: bool(false)
106:
107: Warning: explode(): Empty delimiter in %s on line %d
108: bool(false)
109:
110: Warning: explode(): Empty delimiter in %s on line %d
111: bool(false)
112:
113: Warning: explode(): Empty delimiter in %s on line %d
114: bool(false)
115: -- Iteration 3 --
116: array(1) {
117: [0]=>
118: string(10) "1234NULL23"
119: }
120: array(1) {
121: [0]=>
122: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
123: }
124: array(1) {
125: [0]=>
126: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
127: }
128: array(2) {
129: [0]=>
130: string(10) "1234NULL23"
131: [1]=>
132: string(43) "00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
133: }
134: -- Iteration 4 --
135: array(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: }
147: array(1) {
148: [0]=>
149: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
150: }
151: array(1) {
152: [0]=>
153: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
154: }
155: array(2) {
156: [0]=>
157: string(14) "1234NULL23abcd"
158: [1]=>
159: string(42) "0000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
160: }
161: -- Iteration 5 --
162: array(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: }
174: array(1) {
175: [0]=>
176: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
177: }
178: array(1) {
179: [0]=>
180: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
181: }
182: array(2) {
183: [0]=>
184: string(14) "1234NULL23abcd"
185: [1]=>
186: string(42) "0000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
187: }
188: -- Iteration 6 --
189: array(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: }
201: array(1) {
202: [0]=>
203: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
204: }
205: array(1) {
206: [0]=>
207: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
208: }
209: array(2) {
210: [0]=>
211: string(0) ""
212: [1]=>
213: string(56) "234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
214: }
215: -- Iteration 7 --
216:
217: Warning: explode(): Empty delimiter in %s on line %d
218: bool(false)
219:
220: Warning: explode(): Empty delimiter in %s on line %d
221: bool(false)
222:
223: Warning: explode(): Empty delimiter in %s on line %d
224: bool(false)
225:
226: Warning: explode(): Empty delimiter in %s on line %d
227: bool(false)
228: -- Iteration 8 --
229: array(2) {
230: [0]=>
231: string(28) "1234NULL23abcd00000TRUEFALSE"
232: [1]=>
233: string(12) "1.234444true"
234: }
235: array(1) {
236: [0]=>
237: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
238: }
239: array(1) {
240: [0]=>
241: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
242: }
243: array(2) {
244: [0]=>
245: string(28) "1234NULL23abcd00000TRUEFALSE"
246: [1]=>
247: string(27) "1.234444true-11.24%PHP%ZEND"
248: }
249: -- Iteration 9 --
250: array(1) {
251: [0]=>
252: string(28) "1234NULL23abcd00000TRUEFALSE"
253: }
254: array(1) {
255: [0]=>
256: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
257: }
258: array(1) {
259: [0]=>
260: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
261: }
262: array(2) {
263: [0]=>
264: string(28) "1234NULL23abcd00000TRUEFALSE"
265: [1]=>
266: string(23) "4444true-11.24%PHP%ZEND"
267: }
268: -- Iteration 10 --
269: array(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: }
283: array(1) {
284: [0]=>
285: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
286: }
287: array(1) {
288: [0]=>
289: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
290: }
291: array(2) {
292: [0]=>
293: string(3) "123"
294: [1]=>
295: string(53) "NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
296: }
297: -- Iteration 11 --
298: array(2) {
299: [0]=>
300: string(48) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24"
301: [1]=>
302: string(3) "PHP"
303: }
304: array(1) {
305: [0]=>
306: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
307: }
308: array(1) {
309: [0]=>
310: string(57) "1234NULL23abcd00000TRUEFALSE-11.234444true-11.24%PHP%ZEND"
311: }
312: array(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() --
322: array(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() --
330: array(1) {
331: [0]=>
332: string(29) "Name:Phone:Address:City:State"
333: }
334: array(1) {
335: [0]=>
336: string(29) "Name:Phone:Address:City:State"
337: }
338:
339: *** Testing explode() for maximum limit of string with Limit = -1 ***
340: array(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 --
448: array(2) {
449: [0]=>
450: string(4) "1234"
451: [1]=>
452: string(10) "5678
453: 9100"
454: }
455: array(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 --
467: array(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: }
477: array(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 ***
495: array(2) {
496: [0]=>
497: string(1) "O"
498: [1]=>
499: string(4) "ject"
500: }
501:
502: *** Testing error conditions ***
503:
504: Warning: explode() expects at most 3 parameters, 4 given in %s on line %d
505: NULL
506:
507: Warning: explode() expects at least 2 parameters, 1 given in %s on line %d
508: NULL
509: Done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>