突出显示单词末尾的单词

发布于 2024-09-03 05:29:59 字数 460 浏览 1 评论 0原文

我不确定如何更好地表达标题,但我的问题是突出显示功能不会突出显示单词末尾的搜索关键字。例如,如果搜索关键字是“self”,则会突出显示“self”或“self-less”或“Self”[大写S],但不会突出显示“yourself”或“himself”等自我。 。

这是突出显示的功能:

function highlightWords($text, $words) {
    preg_match_all('~\w+~', $words, $m);
    if(!$m)
        return $text;
    $re = '~\\b(' . implode('|', $m[0]) . ')~i';
    $string = preg_replace($re, '<span class="highlight">$0</span>', $text);

    return $string;
}

i'm not sure how i could have phrased the title better, but my issue is that the highlight function doesn't highlight the search keywords which are at the end of the word. for example, if the search keyword is 'self', it will highlight 'self' or 'self-lessness' or 'Self' [with capital S] but it will not highlight the self of 'yourself' or 'himself' etc. .

this is the highlight function:

function highlightWords($text, $words) {
    preg_match_all('~\w+~', $words, $m);
    if(!$m)
        return $text;
    $re = '~\\b(' . implode('|', $m[0]) . ')~i';
    $string = preg_replace($re, '<span class="highlight">$0</span>', $text);

    return $string;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

π浅易 2024-09-10 05:29:59

看来您的正则表达式开头可能有一个 \b ,这意味着单词边界。由于“yourself”中的“self”不是从单词边界开始,因此它不匹配。去掉\b

It seems you might have a \b at the beginning of your regex, which means a word boundary. Since the 'self' in 'yourself' doesn't start at a word boundary, it doesn't match. Get rid of the \b.

笑咖 2024-09-10 05:29:59

尝试这样的事情:

function highlight($text, $words) {
    if (!is_array($words)) {
        $words = preg_split('#\\W+#', $words, -1, PREG_SPLIT_NO_EMPTY);
    }
    $regex = '#\\b(\\w*(';
    $sep = '';
    foreach ($words as $word) {
        $regex .= $sep . preg_quote($word, '#');
        $sep = '|';
    }
    $regex .= ')\\w*)\\b#i';
    return preg_replace($regex, '<span class="highlight">\\1</span>', $text);
}

$text = "isa this is test text";
$words = array('is');

echo highlight($text, $words);  // <span class="highlight">isa</span> <span class="highlight">this</span> <span class="highlight">is</span> test text

循环是为了正确引用每个搜索词...

编辑:修改函数以在 $words 参数中采用字符串或数组。

Try something like this:

function highlight($text, $words) {
    if (!is_array($words)) {
        $words = preg_split('#\\W+#', $words, -1, PREG_SPLIT_NO_EMPTY);
    }
    $regex = '#\\b(\\w*(';
    $sep = '';
    foreach ($words as $word) {
        $regex .= $sep . preg_quote($word, '#');
        $sep = '|';
    }
    $regex .= ')\\w*)\\b#i';
    return preg_replace($regex, '<span class="highlight">\\1</span>', $text);
}

$text = "isa this is test text";
$words = array('is');

echo highlight($text, $words);  // <span class="highlight">isa</span> <span class="highlight">this</span> <span class="highlight">is</span> test text

The loop, is so that every search word is properly quoted...

EDIT: Modified function to take either string or array in $words parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文