JSFiddle - React, Tailwind, and code Playground

PHP function to search and highlight keyword in a string

by Jennifer Perrin

JavaScript

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

function highlighter_text($text, $words)
{
    $split_words = explode( " " , $words );
    foreach($split_words as $word)
    {
        $color = "#4285F4";
        $text = preg_replace("|($word)|Ui" ,
            "<span style=\"color:".$color.";\"><b>$1</b></span>" , $text );
    }
    return $text;
}

echo highlighter_text( $string ,$words );