<?php

 

   $sentense = 'I love programming. Basically php programming.';
    $search_keyword = 'love php programming';

 

   //call hightlight keyword class

 


    $word_highlight_obj = new word_search_and_highlight($sentense , $search_keyword);


    echo $word_highlight_obj->result;

 

 

?>

 

 <?php


    class word_search_and_highlight
    {
        public $result;
 
        function __construct($text, $words)
        {
            $split_words = explode( " " , $words );
            foreach ($split_words as $word)
            {
                $color = self::generate_colors();
                $text = preg_replace("|($word)|Ui" ,
                           "<span style=\"background:".$color.";\"><b>$1</b></span>" , $text );
            }
            $this->result = $text;
        }
 
        private function rgbhex($red, $green, $blue)
        {
            return sprintf('#%02X%02X%02X', $red, $green, $blue);
        }
 
        private function generate_colors()
        {
            $red = rand( rand(60,100) , rand(200,252) );
            $green = rand( rand(60,100) , rand(200,252) );
            $blue = rand( rand(60,100) , rand(200,252) );
 
            $color = self::rgbhex( $red , $green , $blue );
            return $color;
        }
    }

 

 

?>