Search engine keyword highlighter.
11
Nico
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;
}
?>
Comments
Mon. Feb. 12th, 2007 2:53 AM
Nico
Nico
Thu. Aug. 9th, 2007 1:28 AM
bebelino007
bebelino007





