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