highlight search text in php 1

highlight search text in php

function highlightWords($text, $word){
    $text = preg_replace('#'. preg_quote($word) .'#i', '\\0', $text);
    return $text;

//function calling
highlightWords($desc, $search_word);

Here is what the above code is Doing:
1. The highlightWords() function takes two parameters: $text and $word.
2. The preg_replace() function searches for the $word in the $text and replaces it with the same word wrapped in a span tag with a background color.
3. The preg_quote() function escapes the $word so that it can be used in the regular expression.
4. The highlightWords() function is called with the $desc and $search_word as parameters.

Similar Posts