i'm trying to get this code to highlight several words in a large text.
now the problem that an other piece of code that i use to change some words to links is giving problems when i try to highlight multiple words at the same time. the problem only occurs if one of the words that need to be highlighted is one of the words that needs to be replaced by the link.
this my code
//get searched tag from url
if( preg_match('/^[a-zA-Z0-9]/', $_GET['highl'] )){
$highl = $_GET['highl'];
}
//replace words that are also tags to tag-links
$query = mysql_query("SELECT DISTINCT tag FROM sectags");
while ($rowedit = mysql_fetch_assoc($query))
{
$tag = $rowedit['tag'];
if ($tag != $highl){ //if search highlighted dont replace to link
$link = "<a href=\"?tag={$tag}\">{$tag}</a>";
$edited = str_replace($tag, $link, $edited);
}
}
$post = $edited;
//highlight searched words
if (!empty($highl)){
$post = highlight($post,$highl);
}
this is the function i use
function highlight($strtobold, $keywords) {
$patterns = Array(); $replaces = Array();
if ($keywords != "") {
$words=explode(" ", $keywords);
foreach($words as $word){
$patterns[]='/'.$word.'/i';
$replaces[]='<FONT style="BACKGROUND-COLOR: #53abd5; COLOR: WHITE">$0</FONT>';
}
return preg_replace($patterns, $replaces, $strtobold);
} else return $strtobold;
}
example;
in needs to be changed to a link
if i try to highlight for example in and an otherword i get this:
adm<a href="?tag=<FONT style="BACKGROUND-COLOR: #53abd5; COLOR: WHITE">in</FONT>"><FONT style="BACKGROUND-COLOR: #53abd5; COLOR: WHITE">in</FONT></a>
if the word is admin
thx