Php blow string using regex

I have a long line consisting of a series of sentences separated by a single quote.

Example:

This\ sentence number 1'This\ sentence number 2'

Note that the string has single quotes, part of the sentence itself, which is escaped. I need to explode a string using a single quote, but not an escaped single quote.

The output should be:

Array{
      [0]=>This\ sentence number 1
      [1]=>This\ sentence number 2
}

Basically, I need to blow the string at {'}, but not {\'}. Thanks in advance.

+5
source share
1 answer

Try the following:

print_r(preg_split("/(?<!\\\)\'/", "This\ sentence number 1'This\ sentence number 2'"));
+10
source

All Articles