Highlight text, except html tags

2019-02-22 06:26发布

I'm using the code below to highlight some keywords in a text:

$message = str_ireplace($words,'<span class="hightlighted_text">'.$words.'</span>',$message);

The text may contain some html tags, for example <img>, <strong>, etc..

How can I highlight "normal" text, except the text between the html tags? Because when users search for "img" the <img> text will be highlighted and the image doesn't work anymore.

3条回答
疯言疯语
2楼-- · 2019-02-22 06:31

Use a DOM parser of some sort. This is not something you want to do with regex.

查看更多
beautiful°
3楼-- · 2019-02-22 06:43

From http://forum.phpfrance.com/vos-contributions/remplacement-selectif-hors-dans-balises-html-t199.html

function mon_rplc_callback($capture){
  global $arg;
  return ($arg['flag'] == 1)
  ? $arg['fct']($arg['from'], $arg['to'], $capture[1]).$capture[2]
  : $capture[1].$arg['fct']($arg['from'], $arg['to'], $capture[2]);
}

function split_tag($from, $to, $txt, $fct, $flag = 1){
  global $arg;
  $arg = compact('from', 'to', 'fct', 'flag');
  return preg_replace_callback('#((?:(?!<[/a-z]).)*)([^>]*>|$)#si', "mon_rplc_callback", $txt);
}

When $flag == 1, the replacement function is applied outside HTML. When $flag == -1, the replacement function is applied inside HTML.

Applied to your example, it would give something like this:

echo split_tag($words, '<span class="hightlighted_text">'.$words.'</span>', $message, 'str_ireplace', 1);

Enjoy! ;)

查看更多
再贱就再见
4楼-- · 2019-02-22 06:49

Better code based on reply from @Savageman

$str = '<a href="ba">ba</a>';
$highlightWhat = "ba";
$str = preg_replace_callback('#((?:(?!<[/a-z]).)*)([^>]*>|$)#si', function($m) use ($highlightWhat) {
            return preg_replace('~('.$highlightWhat.')~i', '<span style="background:#fff330">$1</span>', $m[1]) . $m[2];
        },
        $str);
查看更多
登录 后发表回答