我想用替换句来编排句子,而不是在双引号之间。
示例,这句话:
$remplacement = ['the', 'in']; // words who must be deleted
$sentence = "the sea" OR the sea AND "the sea" in the world.应该是:
“海”或“海”和“海”世界。
我认为我们可以使用preg_replace或preg_match。我找到了在双引号上引用文本的regex:
'/[^"]+(?=(?:[^"]*"[^"]*"[^"]*)*$)/'发布于 2018-09-24 16:44:01
您可以忽略引用的单词与PCRE动词。然后,你可以用一个“或”的内爆术语,只需替换它们。您应该使用单词边界,这样部分匹配就不会被替换。
$remplacement = ['the', 'in'];
$sentence = '"the sea" OR the sea AND "the sea" in the world.';
echo preg_replace('/([\'"]).*?\1(*SKIP)(*FAIL)|\b(' . implode ('|', $remplacement) . ')\b/', '', $sentence);但是你的例子是不正确的,对吗?最后,the world也应该变成world。
如果您关心的是双间距,您可以使用:
preg_replace('/\h\h+/', ' ',才能摆脱它。
https://stackoverflow.com/questions/52483588
复制相似问题