So, I have this wordpress driven site, and I have a plugin written that runs when a post is viewed.

The content of the post is loaded into $content.

I also have a database of keywords and links to other posts on my site.

I want to search the $content for any matches to any of the keywords, and replace the text with the the link associated with the matching keyword.

BUT I can't just use str_replace(); because sometimes matching keywords also match image file names. Big problem.

I'm kind of stumped, does an existing function allow for a simplistic search and replace without disturbing my existing html tags?

Code Sample:

<?php


function inter_link($content){

// $content contains post content

//Loop through keywords and see if the post contains any matches
$ck = mysql_query("SELECT * FROM wp_wpinterlink");
$cck = mysql_num_rows($ck);
if($cck > 0){
while($ack = mysql_fetch_array($ck)){
extract($ack);
/*   MySQL Schema   */
/* id keywords link */

$kws = explode(",",$keywords);
if(count($kws) > 0){
foreach($kws as $kk => $vv){
$rpl = "<a href=\"$link\">$vv</a>";

// Does not work because it messes up image tags and such
//$content = str_replace($vv,$rpl,$content);


} // end foreach
} // end count $kws
} // end while
} // end $cck > 0

return($content);

} // end function


?>

Many thanks for your input

Technically, a regex may get you on your way, but it will not cover all options.

I have a javascript example somewhere that highlights words in text, but not in tags. I'll see if I can find it. Perhaps it will suffice for your issue.

Thank you, i'm still looking for answers on my end. I tried using a regular expression and it used too much memory or something for some reason, I don't know regex very well.