Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php突出显示多个单词_Php_Highlight - Fatal编程技术网

php突出显示多个单词

php突出显示多个单词,php,highlight,Php,Highlight,我要高光多字 我想搜索所有搜索词,即使它们重叠 如果我搜索ab和bc,abc将突出显示使用这行代码,这有助于突出显示搜索字符串: Use this line of code which helps to highlights the search string: <?php function highlightsWords($text, $words) { preg_match_all('~\w+~', $words, $m); if(!$m) return $text; $r

我要高光多字

我想搜索所有搜索词,即使它们重叠

如果我搜索ab和bc,abc将突出显示

使用这行代码,这有助于突出显示搜索字符串:
Use this line of code which helps to highlights the search string:
<?php
function highlightsWords($text, $words) {
  preg_match_all('~\w+~', $words, $m);
  if(!$m) return $text;
  $re = '~\\b(' . implode('|', $m[0]) . ')\\b~i';
  return preg_replace($re, '<b>$0</b>', $text);
}

$text = 'Hang Seng Index advanced 0.6% to 24,400.80, after falling as much as 0.4% earlier';
$words = 'hang earlier';
echo highlightsWords($text, $words);
?>

对不起,不清楚你到底问了什么。这个问题是关于搜索还是关于突出显示?请花点时间修改您的问题,并详细解释您真正想要做的事情。如果您想突出显示ab word,然后应用css for spanI change question@arkaschaGood方法,但根据要求,部分单词将失败。此外,您还需要引用搜索词以防止正则表达式中断。