Home » Php » php – highlight the word in the string, if it contains the keyword

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

Posted by: admin April 23, 2020 Leave a comment


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

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

but it menshions only keyword. my bird is funny

How to&Answers:

Try this:

preg_replace("/\w*?$keyword\w*/i", "<b>$0</b>", $str)

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

And I recommend you to use preg_quote to escape the keyword:

preg_replace("/\w*?".preg_quote($keyword)."\w*/i", "<b>$0</b>", $str)

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

preg_replace("/\p{L}*?".preg_quote($keyword)."\p{L}*/ui", "<b>$0</b>", $str)


You can do the following:

 $str = preg_replace("/\b([a-z]*${keyword}[a-z]*)\b/i","<b>$1</b>",$str);


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


$str = "my bird is funny";

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

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

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

echo $str;


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

$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>$0</b>",$str);
echo $str;


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

<?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;
    } ?>