note 75405 deleted from function.preg-replace by nlopess
| From: | nlopess@php.net | Date: | Tue, 19 Jun 2007 12:07:27 +0000 |
| Subject: | note 75405 deleted from function.preg-replace by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127737@lists.php.net to get a copy of this message | ||
Note Submitter: Bob Jones
----
Heres a great script that I use for my search engine to make keywords appear bold but it can be used
for other uses.
<?php
/*
$keywords = Keywords to highlight
$message = String to highlight the keywords in
$stag = Starting HTML tag for the highlighted string
$etag = Ending HTML tag for the highlighted string
*/
function do_highlight($keywords, $message, $stag = "<b>", $etag =
"</b>"){
preg_match_all("/\"(.*?)\"/", $keywords, $quotes);
foreach($quotes[1] as $quot){
$terms[] = $quot;
}
$keywords = preg_replace("/\".*?\"/", "", $keywords);
$keywords = preg_replace("/\"/", "", $keywords);
$keywords = preg_replace("/ /", " ", $keywords);
$terms = array_merge($terms, explode(" ", $keywords));
foreach($terms as $term){
if(trim($term)){
$message = preg_replace('#(?<=[^\w=])('.$term.')(?=[^\w=])#siU',
$stag.'\\1'.$etag, $message);
}
}
return $message;
}
echo do_highlight('"steve miller band" airliner 1977 paul', "\"Jet
Airliner\" is a song by the Steve Miller band released in 1977 the song was written by Paul
Pena. The song describes a man who is looking for a new life so he sings how he can barely get on a
\"Big ole' Jet Airliner\".");
?>