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

时间:2018-12-15 20:54:24

标签: php preg-replace

我要替换为以下字符串:

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

但是您可以看到它在替换it时将替换its,同时也在'中替换doesn't

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

P.S:另外,我还要求它{strong>不区分大小写:/i的工作原理很滑稽:(

谢谢!

1 个答案:

答案 0 :(得分:1)

将正则表达式更改为此:

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

|\b中的管道对我来说没有意义,也许是拼写错误。 \B之后的附加\W将确保仅当非字母字符没有紧随字母字符后才被替换。这没有您要求的限制那么严格,但在其他情况下也很有用,例如带有连字符的单词(例如岳母)。