搜索“;全词匹配”;并在PHP中突出显示该单词

搜索“;全词匹配”;并在PHP中突出显示该单词,php,preg-replace-callback,Php,Preg Replace Callback,我需要帮助找出PHP preg_replace_回调函数中的匹配词。如何从一句话中找出确切的“邀请”一词?我需要强调这个词。 我需要使用此函数的解决方案'preg\u replace\u callback' $search_word = 'Invitation.' $string = '/\b' . preg_quote($search_word) . '\b/i'; preg_replace_callback($string, function($match

我需要帮助找出PHP preg_replace_回调函数中的匹配词。如何从一句话中找出确切的“邀请”一词?我需要强调这个词。 我需要使用此函数的解决方案'preg\u replace\u callback'

$search_word = 'Invitation.'
$string = '/\b' . preg_quote($search_word) . '\b/i'; 
preg_replace_callback($string, 
                function($matches) use (&$counter) {
                   
                   $counter++;
                   return '<b class="search_mark highlighted" id="matched_'. $counter . '">'
                       . substr($matches[0], 0, strlen($matches[0]))
                    
                       . '</b>' ;
            }, $text);
$search\u word='Invitation'
$string='/\b'。preg_quote($search_word)。'\b/i';
preg_replace_回调($string,
函数($matches)使用(&$counter){
$counter++;
返回“”
.substr($matches[0],0,strlen($matches[0]))
. '' ;
}美元文本);

您正在查找单词边界内的单词,但您的
$search\u单词
末尾有一个句点。也就是说,它永远不可能在单词范围内(除非新单词在句号之后开始)。如果要匹配任何包含句点的字符串,则不应通过
\b
@AterLux yes限制搜索。我正在搜索单词,当时四个类别使用了匹配大小写、仅匹配整个单词、环绕、正则表达式。所以它是动态搜索。用户可以选择普通搜索或上述任何搜索类别。所以我在搜索整个单词时遇到了困难。可能尝试在前后匹配单词边界或空白字符?如果这对你来说不合适,那么你需要更具体地描述你的实际需求。我认为你所说的“单词”的意思有点混乱“邀请”是一个单词,“邀请”是一个单词加上标点符号。还有一句话的意思(至少用英语)。如果它是一个正常格式的句子,你可以假设。结束句子,在这种情况下,在正则表达式中使用$可能会有所帮助,等等。