php 突出显示字符串中的单词(如果它包含关键字)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2483844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:39:38  来源:igfitidea点击:

highlight the word in the string, if it contains the keyword

php

提问by Simon

how write the script, which menchion the whole word, if it contain the keyword? example:keyword "fun", string - the bird is funny, result - the bird is * funny*. i do the following

如果包含关键字,如何编写脚本,整个单词哪个menchion?例如:关键字“有趣”,字符串 - 鸟很有趣,结果 - 鸟很有趣*。我做以下

     $str = "my bird is funny";
     $keyword = "fun";
     $str = preg_replace("/($keyword)/i","<b></b>",$str);

but it menshions only keyword. my bird is funny

但它只提及关键字。我的鸟很有趣纽约

回答by Gumbo

Try this:

尝试这个:

preg_replace("/\w*?$keyword\w*/i", "<b>
preg_replace("/\w*?".preg_quote($keyword)."\w*/i", "<b>
preg_replace("/\p{L}*?".preg_quote($keyword)."\p{L}*/ui", "<b>
 $str = preg_replace("/\b([a-z]*${keyword}[a-z]*)\b/i","<b></b>",$str);
</b>", $str)
</b>", $str)
</b>", $str)

\w*?matches any word characters before the keyword (as least as possible) and \w*any word characters after the keyword.

\w*?匹配关键字之前的任何单词字符(尽可能)和\w*关键字之后的任何单词字符。

And I recommend you to use preg_quoteto escape the keyword:

我建议您使用preg_quote来转义关键字:

$str = "Its fun to be funny and unfunny";
$keyword = 'fun';
$str = preg_replace("/\b([a-z]*${keyword}[a-z]*)\b/i","<b></b>",$str);
echo "$str"; // prints 'Its <b>fun</b> to be <b>funny</b> and <b>unfunny</b>'

For Unicode support, use the uflag and \p{L}instead of \w:

对于 Unicode 支持,请使用u标志而\p{L}不是\w

<?php 
$str = "my bird is funny";

$keyword = "fun";
$look = explode(' ',$str);

foreach($look as $find){
    if(strpos($find, $keyword) !== false) {
        if(!isset($highlight)){ 
            $highlight[] = $find;
        } else { 
            if(!in_array($find,$highlight)){ 
                $highlight[] = $find;
            } 
        }
    }   
} 

if(isset($highlight)){ 
    foreach($highlight as $replace){
        $str = str_replace($replace,'<b>'.$replace.'</b>',$str);
    } 
} 

echo $str;
?>

回答by codaddict

You can do the following:

您可以执行以下操作:

$keyword = ".in#.com#dot.com#1#2#3#4#5#6#7#8#9#one#two#three#four#five#Six#seven#eight#nine#ten#dot.in#dot in#";

$keyword = implode('|',explode('#',preg_quote($keyword)));

$str = "PHP is dot .com the amazon.in 123455454546 dot in scripting language of choice.";

$str = preg_replace("/($keyword)/i","<b>
<?php $body_text='This is simple code for highligh the word in a given body or text';  //this is the body of your page
$searh_letter = 'this';  //this is the string you want to search for
$result_body = do_Highlight($body_text,$searh_letter);    // this is the result with highlight of your search word
echo $result_body;            //for displaying the result
function do_Highlight($body_text,$searh_letter){     //function for highlight the word in body of your page or paragraph or string
     $length= strlen($body_text);    //this is length of your body
     $pos = strpos($body_text, $searh_letter);   // this will find the first occurance of your search text and give the position so that you can split text and highlight it
     $lword = strlen($searh_letter);    // this is the length of your search string so that you can add it to $pos and start with rest of your string
     $split_search = $pos+$lword; 
     $string0 = substr($body_text, 0, $pos); 
     $string1 = substr($body_text,$pos,$lword);
     $string2 = substr($body_text,$split_search,$length);
     $body = $string0."<font style='color:#FF0000; background-color:white;'>".$string1." </font> ".$string2;
     return $body;
    } ?>
</b>",$str); echo $str;

Example:

例子:

##代码##

回答by Kevin

##代码##

回答by Johnson

Here by am added multi search in a string for your reference

在这里我在字符串中添加了多搜索以供您参考

##代码##

回答by vishal_g

Search and highlight the word in your string, text, body and paragraph:

搜索并突出显示字符串、文本、正文和段落中的单词:

##代码##