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