PHP explode string using a regular expression

2019-06-21 17:55发布

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

Example:

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

Notice that the string has single quotes part of the sentence itself which are escaped. I need to explode the string using single quote, but not the escaped single quote.

The output should be:

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

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

1条回答
戒情不戒烟
2楼-- · 2019-06-21 18:30

Try this:

print_r(preg_split("/(?<!\\\)\'/", "This\'s sentence number 1'This\'s sentence number 2'"));
查看更多
登录 后发表回答