1

此函数在文本中搜索单词(来自 $words 数组)并突出显示它们。

function highlightWords(Array $words, $text){ // Loop through array of words
    foreach($words as $word){ // Highlight word inside original text
        $text = str_replace($word, '<span class="highlighted">' . $word . '</span>', $text);
    }         
    return $text; // Return modified text
}

这是问题所在:

假设 $words = array("car", "drive");

有没有办法让该功能不仅突出显示汽车一词,还突出包含字母“汽车”的单词,例如:汽车、汽车等。

谢谢!

4

3 回答 3

1

你想要的是一个正则表达式,特别是 preg_replace 或 peg_replace_callback (建议在你的情况下使用回调)

<?php
$searchString = "The car is driving in the carpark, he's not holding to the right lane.\n";

// define your word list
$toHighlight = array("car","lane");

因为您需要一个正则表达式来搜索您的单词,并且您可能希望或需要随时间变化或变化,所以将其硬编码到您的搜索词中是不好的做法。因此,最好使用 array_map 遍历数组并将搜索词转换为正确的正则表达式(这里只是用 / 括起来并添加“接受所有内容直到标点符号”表达式)

$searchFor = array_map('addRegEx',$toHighlight);

// add the regEx to each word, this way you can adapt it without having to correct it everywhere
function addRegEx($word){
    return "/" . $word . '[^ ,\,,.,?,\.]*/';
}

接下来,您希望将找到的单词替换为突出显示的版本,这意味着您需要进行动态更改:使用 preg_replace_callback 而不是常规的 preg_replace ,以便它为找到的每个匹配项调用一个函数并使用它来生成正确的结果。在这里,我们将找到的单词包含在其 span 标签中

function highlight($word){
    return "<span class='highlight'>$word[0]</span>";
}

$result = preg_replace_callback($searchFor,'highlight',$searchString);

print $result;

产量

The <span class='highlight'>car</span> is driving in the <span class='highlight'>carpark</span>, he's not holding to the right <span class='highlight'>lane</span>.

因此,显然只需将这些代码片段粘贴在另一个之后即可获得工作代码。;)

编辑:下面的完整代码稍作修改=放置在例程中,以便原始请求者使用。+ 不区分大小写

完整代码:

<?php

$searchString = "The car is driving in the carpark, he's not holding to the right lane.\n";
$toHighlight = array("car","lane");

$result = customHighlights($searchString,$toHighlight);

print $result;

// add the regEx to each word, this way you can adapt it without having to correct it everywhere

function addRegEx($word){
    return "/" . $word . '[^ ,\,,.,?,\.]*/i';
}

function highlight($word){
    return "<span class='highlight'>$word[0]</span>";
}

function customHighlights($searchString,$toHighlight){

// define your word list
$searchFor = array_map('addRegEx',$toHighlight);
$result = preg_replace_callback($searchFor,'highlight',$searchString);
return $result;

}
于 2012-05-28T20:45:23.870 回答
0

我还没有测试过,但我认为应该这样做:-

$text = preg_replace('/\W((^\W)?$word(^\W)?)\W/', '<span class="highlighted">' . $1 . '</span>', $text);

这会在一个完整的有界单词中查找字符串,然后使用 preg_replace 和正则表达式将跨度放在整个字符串中。

于 2012-05-28T20:31:09.993 回答
0
function replace($format, $string, array $words)
{
    foreach ($words as $word) {
        $string = \preg_replace(
            sprintf('#\b(?<string>[^\s]*%s[^\s]*)\b#i', \preg_quote($word, '#')),
            \sprintf($format, '$1'), $string);
    }
    return $string;
}


// courtesy of http://slipsum.com/#.T8PmfdVuBcE
$string = "Now that we know who you are, I know who I am. I'm not a mistake! It
all makes sense! In a comic, you know how you can tell who the arch-villain's
going to be? He's the exact opposite of the hero. And most times they're friends,
like you and me! I should've known way back when... You know why, David? Because
of the kids. They called me Mr Glass.";

echo \replace('<span class="red">%s</span>', $string, [
    'mistake',
    'villain',
    'when',
    'Mr Glass',
]);

正因为它使用sprintf周围字符串的格式,您可以相应地更改替换。

请原谅 5.4 语法

于 2012-05-28T21:01:11.410 回答