Consider the snippet:
var_dump(preg_split("/./", "A.B.C")); // split on anything as the dot has not been escaped
which produces the output:
array(6) {
[0]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
}
Can anyone please explain how it works? Also I don’t see A,B or C in the output!! Why ?
preg_splitwill split the input string at all occurrences that match the given regular expression and remove the match. In your case.matches any character (except line breaks). So your input stringA.B.Cwill be splitted at each character giving you six parts where each part is an empty string.If you want to have the matches to be part of the result, you can use either look-around assertions or set the PREG_SPLIT_DELIM_CAPTURE (depending on the result you want to have).