PHP preg_replace:匹配包含破折号的单词

PHP preg_replace:匹配包含破折号的单词,php,regex,Php,Regex,我在搜索字符串中的单词并将其转换为链接时遇到问题。如何进行搜索,将包含“-”的单词视为整个单词,而不是以“-”分隔的两个单词 下面是一个例子,如果我搜索单词“穷举”,我的搜索函数也会匹配单词“非穷举”的部分,尽管我不想匹配这个单词 $string = "Bla bla bla non-exhaustive blabla bla exhaustive"; $words = array('exhaustive','non-exhaustive'); foreach ($words as $word)

我在搜索字符串中的单词并将其转换为链接时遇到问题。如何进行搜索,将包含“-”的单词视为整个单词,而不是以“-”分隔的两个单词

下面是一个例子,如果我搜索单词“穷举”,我的搜索函数也会匹配单词“非穷举”的部分,尽管我不想匹配这个单词

$string = "Bla bla bla non-exhaustive blabla bla exhaustive";
$words = array('exhaustive','non-exhaustive');
foreach ($words as $word) {
    $string = preg_replace('/\b'.$word.'\b/i', '<a href=#>'.$word.'</a>',$string);
}

echo $string;
$string=“Bla-Bla-Bla-non-executive Bla-Bla-Bla-executive”;
$words=array('穷举','非穷举');
foreach($words作为$word){
$string=preg_replace('/\b'.$word'.\b/i','.$string);
}
echo$字符串;
结果:

Bla Bla Bla Bla non-详尽Bla Bla Bla详尽

非常感谢你的帮助

$string=preg_replace('/\b[^\-]'.preg_quote($word)。'\b/i',''$string);
$string = preg_replace('/\b[^\-]' . preg_quote($word) . '\b/i', '<a href=#>' . $word . '</a>',$string);
应该这样做:)


*编辑,Aleks只用了2秒就打败了我:p

你可以明确阻止连字符:
'/\b[^\-].$word.\b/i'
$word
上使用,我会投票给你。:)