PHP搜索文本突出显示功能

PHP搜索文本突出显示功能

本文介绍了PHP搜索文本突出显示功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP高亮功能,可以使某些单词加粗.

I have a PHP highlighting function which makes certain words bold.

下面是该函数,它很好用,除非数组:$ words包含单个值,即:b

Below is the function, and it works great, except when the array: $words contains a single value that is: b

例如某人搜索:jessie j价格标签feat b o b

For example someone searches for: jessie j price tag feat b o b

这将在$ words数组中包含以下条目:jessie,j,price,tag,feat,b,o,b

This will have the following entries in the array $words: jessie,j,price,tag,feat,b,o,b

当出现"b"时,我的整个函数都出错了,并且显示了一大堆错误的html标签.当然,我可以从数组中去除任何'b'值,但这并不理想,因为突出显示在某些查询中无法正常工作.

When a 'b' shows up, my whole function goes wrong, and it displays a whole bunch of wrong html tags. Of course I can strip out any 'b' values from the array, but this isn't ideal, as the highlighting isnt working as it should with certain queries.

此示例脚本:

    function highlightWords2($text, $words)
    {
        $text =  ($text);
        foreach ($words as $word)
        {
            $word = preg_quote($word);

            $text = preg_replace("/\b($word)\b/i", '<b>$1</b>', $text);

        }
        return $text;
    }


$string = 'jessie j price tag feat b o b';

$words = array('jessie','tag','b','o','b');

echo highlightWords2($string, $words);

将输出:

<<<b>b</b>><b>b</b></<b>b</b>>>jessie</<<b>b</b>><b>b</b></<b>b</b>>> j price <<<b>b</b>><b>b</b></<b>b</b>>>tag</<<b>b</b>><b>b</b></<b>b</b>>> feat <<b>b</b>><b>b</b></<b>b</b>> <<b>b</b>>o</<b>b</b>> <<b>b</b>><b>b</b></<b>b</b>>

这仅是因为数组中有"b".

And this only happens because there are "b"'s in the array.

你们能看到我可以更改以使其正常运行的任何内容吗?

Can you guys see anything that I could change to make it work properly?

推荐答案

您的问题是,当您的函数通过并查找所有b使其加粗时,它会看到粗体标签,并且也会尝试对其进行粗体处理.

You problem is that when your function goes through and looks for all the b's to bold it sees the bold tags and also tries to bold them as well.

@symcbean很近,却忘了一件事.

@symcbean was close but forgot one thing.

$string = 'jessie j price tag feat b o b';
$words = array('jessie','tag','b','o','b');

print hl($string, $words);

function hl($inp, $words)
{
  $replace=array_flip(array_flip($words)); // remove duplicates
  $pattern=array();
  foreach ($replace as $k=>$fword) {
     $pattern[]='/\b(' . $fword . ')(?!>)\b/i';
     $replace[$k]='<b>$1</b>';
  }
  return preg_replace($pattern, $replace, $inp);
}

您是否看到添加的((?!>)这是一个否定的前瞻性断言,基本上它说只有在字符串后没有">时才匹配,这是打开粗体和关闭粗体标签.请注意,我只在字符串后检查>",以同时排除开始和结束的粗体标签,因为在字符串的开头查找它不会捕获到结束的粗体标签.上面的代码完全按预期工作.

Do you see this added "(?!>)" that is a negative look ahead assertion, basically it says only match if the string is not followed by a ">" which is what would be seen is opening bold and closing bold tags. Notice I only check for ">" after the string in order to exclude both the opening and closing bold tag as looking for it at the start of the string would not catch the closing bold tag. The above code works exactly as expected.

这篇关于PHP搜索文本突出显示功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:40