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