2

Possible Duplicate:
Best way to parse HTML with PHP
PHP Regular expression to match keyword outside HTML tag <a>

I want to highlight keywords being searched for and have it not replace the HTML tags. I used this function but it replaces the links (anchor tags). Is there a way to highlight the search terms without creating invalid HTML.

function highlightSearch($str,$keyword='')
{
    // $str=text in which I want to search,$keyword is the word which I want to replace.
    $keywords = preg_replace('/\s\s+/i', ' ', strip_tags(trim($keyword))); 
    foreach(array_unique(explode(' ', $keywords)) as $keyword)
    {
        //if(!preg_match("|<[^>]+>(.*)</[^>]+>|Ui",$str))
        $str = str_ireplace($keyword,"<b>".$keyword."</b>",$str);//preg_replace("|($keyword)|Ui", "<b>$1</b>", $str);
        //}
        //$str = str_ireplace( "<b>","<b>", $str);`
        return $str;
    }
}
Community
  • 1
  • 1
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • possible duplice of [php regex to match outside of html tags](http://stackoverflow.com/questions/7891771/php-regex-to-match-outside-of-html-tags) – mario Dec 16 '11 at 07:51

0 Answers0