I'm ok with the very simple regular expressions that I would nornally need to use to design a site with, but when it comes to imtermediate or advanced regEXs.. I get totally lost!
I made a script that pull from my mySQL database adn returns an article or news story. After pulling the PHP script compares the article/news body to a glossary I've setup for terms. I have it working perfectly to match the terms in the glossary... The only issue is it matches all matches whether it's in HTML Tags or not...
I need it so that it won't match any terms found that are in any HTML tags unless it's a '<span>' or '<strong>'...
I've listed the code below and appreciate any suggestions or corrections that can be provided...
/* Pull each glossary term from DB */
Code: Select all
$keywordsArray = array();
$queryA = "SELECT * FROM glossary WHERE deviceGlossary != 'device' ORDER BY title DESC";
$resultA = mysql_query($queryA) or die("Died: ".mysql_error());
/* Run through each term */
while ($rows = mysql_fetch_object($resultA)) {
$keywordsArray[$rows->title] = $rows->id;
$addKeyword = preg_replace("/, /", ",", $rows->add_keywords);
$addKeywords = explode(",", $rows->add_keywords);
for ($y=0; $y<count($addKeywords); $y++) {
$keywordsArray[$addKeywords[$y]] = $rows->id;
}
}
/* Terms loaded into array */
$foundMatches = array_combine($keywordsArray, $idsArray);
$contents = explode(" ", $artMainContents);
/* Foreach term compare to article/news.. and replace in the body (article/news story) */
foreach($keywordsArray as $title=>$titid) {
if ($title != "") {
$artMainContents = preg_replace('#\b('.preg_quote($title,'#').')\b#i',"<a href=\"javascript: nullVoid();\" onClick=\"javascript: showTerm('".$titid."', 'default');\" class=\"glossaryTerm\" title=\"Lookup term in the wireless glossary.\">\\1</a>",$artMainContents);
}
}