Question

I have a string:

$str = "yellow;yellow\;er;yellowest";

And I use preg_split like this:

preg_split("/;/", $str);

But I want to get results like this:

array(
    "yellow",
    "yellow;er",
    "yellowest"
)

But the results is not right.

So actually what I want to do is to split the string at a ; but if the ; has a \ before it, it has to stay just ;, remove the \ and not split it there. So it has to split at ; but not \; and if there is a \; it has to remove the \ character. Something like with strings:

echo "Hello There \"Jacques\"";

Gives:

Hello There "Jacques"

And not just:

Hello There

Was it helpful?

Solution

You can use a negative lookbehind to make sure that ; is not preceded by a \

preg_split('/(?<!\\\);/', $str)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top