3

I am working on a code that highlights certain words, using a regex.

Here it is:

function addRegEx($word){
        return "/\b(\w+)?".$word."(\w+)?\b/i";
    }
    function highlight($word){
        return "<span class=\"highlighted\">".$word[0]."</span>";
    }
    function customHighlights($searchString,$toHighlight){
        $searchFor = array_map('addRegEx',$toHighlight);
        $result = preg_replace_callback($searchFor,'highlight',$searchString);
        return $result;
    }

Lets say I use the function customHighlights to searc for the word "car" in a certain text:

Using the boundary - \b - method, the script searches for the word car in the text.

In the regex, I have added (\w+)? in front and after the word, so the script would match words that contain "car" - cars, sportcars, etc...

The problem is, it messes up the inner html, for example:

This is a great car. Click here for <a href="http://cars.com">more</a>

The script will match the word car in the url of the link above, adding span classes to it and messing up the html.

How would you modify the regex and avoid this?

4

2 回答 2

0

您是否考虑过使用 Javascript 在客户端处理文本突出显示?jQuery 或类似工具可以让您遍历节点以找到突出显示的位置,而不是使用原始 HTML。不过,我对正则表达式帮不上什么忙。

于 2013-08-13T11:52:41.220 回答
0

使用正则表达式在文本的最后一个或开头之后搜索单词>,但在这个和单词之间的部分可能不包含标签 start <

看到这个键盘

代码

<?php
$str = 'This is a great car. Click here for <a href="http://cars.com">more cars</a>';
$word = 'car';
$exp = "/((^|>)[^<]*)(\b(\w+)?".$word."(\w+)?\b)/i";
$repl = "\\1<span class=\"highlighted\">\\3</span>";
var_dump(preg_replace($exp, $repl, $str));
?>

输出

string(141) "This is a great <span class="highlighted">car</span>. Click here for <a href="http://cars.com">more <span class="highlighted">cars</span></a>"
于 2013-08-13T11:52:55.533 回答