Search engine keyword highlighter.





11
Date Submitted Sun. Feb. 11th, 2007 8:01 AM
Revision 1 of 1
Helper Nico
Tags google | highlight | keywords | PHP | search | Yahoo
Comments 2 comments
If a visitor comes to your site from a search engine such as Google or Yahoo, this function will highlight the keywords in a given text the user used for his search.

<?php

$text = 'Example text where the keywords would be highlighted.';

echo highlight_search_keywords($text);

?>
 


<?php

function highlight_search_keywords($text)
{
        @extract(@parse_url($_SERVER['HTTP_REFERER']));
       
        if (preg_match('/((fr|g)oogle|msn|yahoo|alexa)/i', $host) AND preg_match('/[qp]=([^&]+)/i', $query, $keywords))
        {
                $keywords = preg_split('/[\s\+\,]+/', rawurldecode($keywords[1]), -1, PREG_SPLIT_NO_EMPTY);
               
                foreach (array_unique($keywords) AS $keyword)
                {
                        $text = preg_replace('/('. preg_quote($keyword) .')/i', '<span style="color: red; font-weight: bold;">$1</span>', $text);
                }
        }
       
        return $text;
}


?>

 

Nico Oelgart

www.nicoswd.com

Comments

Comments If you vote negative...
Mon. Feb. 12th, 2007 2:53 AM    Helper Nico
Comments tags
Thu. Aug. 9th, 2007 1:28 AM    Newbie bebelino007

Voting