php突出显示多个单词

时间:2017-04-05 10:28:52

标签: php highlight

我想高光多字

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

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

1 个答案:

答案 0 :(得分:1)

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);
?>