MENU
Splitting
preg_split($s1,$s2[,$i1=-1,[$i2]]) splits $s2 into an array of strings, using the regular expression $s1 as the separator. If $i1 is specified, up to $i1 substrings are returned, with the rest of the string being placed in the last substring. $i1=-1,0 or NULL means no limit. If $i2 is set to PREG_SPLIT_NO_EMPTY, only non-empty pieces will be returned. If $2 is set to PREG_SPLIT_DELIM_CAPTURE, parenthesized expression in the pattern will be captured as well. If $i2 is set to PREG_SPLIT_ OFFSET_CAPTURE, the string offset too will be returned for each element.<!DOCTYPE html><html><head></head>
<body><?php
$s = 'XXabYYacadZZ';
print_r(preg_split('/a./',$s));
echo "<br />";
print_r(preg_split('/a./',$s,2));
echo "<br />";
print_r(preg_split('/a./',$s,0,PREG_SPLIT_NO_EMPTY));
echo "<br />";
print_r(preg_split('/a.(..)/',$s,0, PREG_SPLIT_DELIM_CAPTURE));
echo "<br />";
print_r(preg_split('/a./',$s,0, PREG_SPLIT_OFFSET_CAPTURE));
echo "<br />";
?> </body></html>Array ( [0] => XX [1] => YY [2] => [3] => ZZ )
Array ( [0] => XX [1] => YYacadZZ )
Array ( [0] => XX [1] => YY [2] => ZZ )
Array ( [0] => XX [1] => YY [2] => [3] => ad [4] => ZZ )
Array ( [0] => Array ( [0] => XX [1] => 0 )
[1] => Array ( [0] => YY [1] => 4 )
[2] => Array ( [0] => [1] => 8 )
[3] => Array ( [0] => ZZ [1] => 10 ) )