PHP preg_replace 非字母数字字符和选择的连词然后拆分

PHP preg_replace non alphanumerical chars and choosen conjunctions and then split

我想在这个字符串中替换:

This is my Store, it has an amazing design; its creator says it was losing money and he doesn't want to maintain it

'(不是)之外的所有非字母数字字符和所有选定的连词:

is, it, its, the, this, if, so, and

到目前为止,我已经设法获得了这个结果:

Array
(
    [1] => This
    [2] => my
    [3] => Store
    [4] => has
    [5] => an
    [6] => amazing
    [7] => design
    [8] => s
    [9] => creator
    [10] => says
    [11] => was
    [12] => losing
    [13] => money
    [14] => and
    [15] => he
    [16] => doesn
    [17] => t
    [18] => want
    [19] => maintain
)

代码如下:

$string = "This is my Store, it has an amazing design; its creator says it was losing money and he doesn't want to maintain it";
$words = array_filter(preg_split('/\s+/', preg_replace('/\W|\b(it|the|its|is|to)|\b/i', ' ', $string)));

print_r($words);

https://3v4l.org/cLrM4

但如您所见,它在应该替换 its 时替换了 it,并且还在 doesn't.

中替换了 '

有人可以帮助我了解我哪里做错了吗? X_X

P.S:我还需要它 不区分大小写 /i 工作起来很滑稽 :(

谢谢!

将您的正则表达式更改为:

/\W\B|\b(it|the|its|is|to)\b/i

|\b中的管道对我来说没有意义,可能是打字错误。 \W 之后的附加 \B 将确保非字母字符仅在其后没有紧跟字母字符时才被替换。这比您要求的限制更少,但也可能对其他情况有用,例如带有连字符的单词(例如 mother-in-law)。