Php 函数,用于在文本中搜索一个单词并突出显示包含该单词的所有单词

Php 函数,用于在文本中搜索一个单词并突出显示包含该单词的所有单词,php,Php,此函数用于搜索文本中的单词(从$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">' .

此函数用于搜索文本中的单词(从$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
}
函数highlightWords(数组$words,$text){//遍历单词数组
foreach($words作为$word){//突出显示原始文本中的单词
$text=str_replace($word、.$word.'、$text);
}         
return$text;//返回修改后的文本
}
问题是:

假设$words=array(“car”、“drive”)

功能是否有办法不仅突出显示单词car,还突出显示包含字母“car”的单词,如:cars、carmania等。


谢谢大家!

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

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

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

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

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

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

您需要的是一个正则表达式,尤其是preg_replace或peg_replace_回调(建议您使用回调)

接下来,您希望用突出显示的版本替换找到的单词,这意味着您需要进行动态更改:使用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;
功能突出显示($word){
返回“$word[0]”;
}
$result=preg_replace_回调($searchFor,'highlight',$searchString);
打印$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;

}

您需要的是正则表达式,尤其是preg\u replace或peg\u replace\u回调(建议您使用回调)

接下来,您希望用突出显示的版本替换找到的单词,这意味着您需要进行动态更改:使用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;
功能突出显示($word){
返回“$word[0]”;
}
$result=preg_replace_回调($searchFor,'highlight',$searchString);
打印$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;

}
函数替换($format,$string,array$words)
{
foreach($words作为$word){
$string=\preg\u replace(
sprintf('#\b(?[^\s]*%s[^\s]*)\b#i',\preg#u quote($word,'.'),
\sprintf($format,$1'),$string);
}
返回$string;
}
//承蒙http://slipsum.com/#.T8PmfdVuBcE
$string=“现在我们知道你是谁了,我知道我是谁了。我没有错!我知道
这一切都是有道理的!在漫画中,你知道如何分辨谁是大反派
将来呢?他和英雄完全相反而且他们大多数时候都是朋友,
就像你和我!我早该知道…你知道为什么吗,大卫?因为
他们叫我格拉斯先生。”;
echo\replace('%s',$string[
“错误”,
“恶棍”,
“什么时候”,
“格拉斯先生”,
]);
因为它对周围的字符串使用了
sprintf
格式,所以您可以相应地更改替换内容

请原谅5.4语法

函数替换($format、$string、array$words)
{
foreach($words作为$word){
$string=\preg\u replace(
sprintf('#\b(?[^\s]*%s[^\s]*)\b#i',\preg#u quote($word,'.'),
\sprintf($format,$1'),$string);
}
返回$string;
}
//承蒙http://slipsum.com/#.T8PmfdVuBcE
$string=“现在我们知道你是谁了,我知道我是谁了。我没有错!我知道
这一切都是有道理的!在漫画中,你知道如何分辨谁是大反派
将来呢?他和英雄完全相反而且他们大多数时候都是朋友,
就像你和我!我早该知道…你知道为什么吗,大卫?因为
他们叫我格拉斯先生。”;
echo\replace('%s',$string[
“错误”,
“恶棍”,
“什么时候”,
“格拉斯先生”,
]);
因为它对周围的字符串使用了
sprintf
格式,所以您可以相应地更改替换内容


请原谅5.4语法

让我来测试一下,我是个新手,一百万年来都不会想到这一点:)小心,我只是把“w”改为“w”来捕捉单词边界!嗯,dreamweaver说php有问题syntax@webmasters这不适用于单引号,您需要将它们替换为双引号,以便使用
$word
值:
“/\W(^\W)?$word(^\W)?\W/“
。注意:此正则表达式不支持UTF-8-\W等同于[^a-zA-Z0-9]。关于UTF-8感知边界的讨论,请看,让我来测试一下,我是这样一个新手,一百万年来都不会想到这一点:)小心,我只是将“w”改为“w”来捕捉单词边界!嗯,dreamweaver说php有问题syntax@webmasters这不适用于单引号,您需要将其替换为双引号,以便
$word