Preg_replace用两个相似的词代替


preg_replace with two similar words

我对斯洛伐克语中两个非常相似的表达有一个问题。我试图替换html链接的表达式,这是我下面的示例代码。数组中大约有90个单词。

// mysql query above.
for($i=1;$i<=$datas[1];$i++) {
  $regex[] = '/('.htmlentities($rows['title'], ENT_QUOTES, 'UTF-8').')/i';
  $wrl[] = '<a href="'.URL.'link/'.urlencode($rows['alias']).'" class="underscored">'.strtolower($rows['title']).'</a>';
}
$content = preg_replace($regex, $wrl, $content);

但是我对非常相似的词有一个问题;(neasytenne mastne kyseling, nasytenne mastne kyseling)。preg_replace从较长的单词中删除ne,这将导致该单词被链接到错误的文章。

尝试修改你的正则表达式,在你想用链接替换的短语之前和之后指定一个词边界'b:

$regex[] = '/'b('.htmlentities($rows['title'], ENT_QUOTES, 'UTF-8').')'b/i';

您可以使用'W:

来前置和追加您的正则表达式。
$regex[] = '/'W('.htmlentities($rows['title'], ENT_QUOTES, 'UTF-8').')'W/i';