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