Php 在术语的上下文中,如何创建指向类别页面的链接?

Php 在术语的上下文中,如何创建指向类别页面的链接?,php,hyperlink,preg-replace,Php,Hyperlink,Preg Replace,我是一名作家 我想在文章的任何地方,我的话被分类,这个词是自动链接到类别 我是这样做的 $text = preg_replace('(hello|word1|word2)', '<a href="'.$link1.'">word</a>', $text); $text = preg_replace('(word3|hello word|word4)', '<a href="'.$link2.'">word</a>', $text); $text=

我是一名作家 我想在文章的任何地方,我的话被分类,这个词是自动链接到类别 我是这样做的

$text = preg_replace('(hello|word1|word2)', '<a href="'.$link1.'">word</a>', $text);
$text = preg_replace('(word3|hello word|word4)', '<a href="'.$link2.'">word</a>', $text);
$text=preg_replace('(hello | word1 | word2)',$text);
$text=preg_replace('(word3 | hello word | word4)',$text);

但是,当“hello world”这个词出现在纸上时,“hello”是指向“$link1”的链接,“word”不能链接

您可以使用负前瞻
(?!)
功能在第一个regexp上不匹配
hello
字符串,然后是
world
字符串,如下示例:

$text = preg_replace('(hello(?! world)|word1|word2)', '<a href="'.$link1.'">word</a>', $text);
$text = preg_replace('(word3|hello world|word4)', '<a href="'.$link2.'">word</a>', $text);
$text=preg_replace(“(hello(?!world)| word1 | word2)”,$text);
$text=preg_replace(“(word3 | hello world | word4)”,$text);
请注意,您的第二个regexp中有一个输入错误:
helloword
而不是
helloworld

参考资料: