Im trying to split string in PHP. I should split string using two delimiters: new line and comma. My code is:
$array = preg_split("/
|,/", $str)
But i get string split using comma, but not using . Why is that? Also , do I have to take into account " " symbol?
I can think of two possible reasons that this is happening.
1.
You are using a single quoted string:
$array = preg_split("/
|,/", 'foo,bar
baz');
print_r($array);
Array
(
[0] => foo
[1] => bar
baz
)
If so, use double quotes "
instead ...
$array = preg_split("/
|,/", "foo,bar
baz");
print_r($array);
Array
(
[0] => foo
[1] => bar
[2] => baz
)
2.
You have multiple newline sequences and I would recommend using \R
if so. This matches any Unicode newline sequence that is in the ASCII
range.
$array = preg_split('/\R|,/', "foo,bar
baz
quz");
print_r($array);
Array
(
[0] => foo
[1] => bar
[2] => baz
[3] => quz
)