Issue Replacing Keywords In Current Page
Posted: Wed Nov 02, 2005 9:17 pm
Can any one please tell me how to search for and replace only whole words?
It's driving me crazy!
I created a glossary of wireless terms and I made a sniplet of code to search each article and/or news story as it's being displayed for the user. The issue I'm having now is that it either doesn't replace whole words if there is a keyword that is similar; ex: keywords: 3G & 3GPP & string: The 3GPP Group; will replace 3G and not 3GPP.
I would appreciate any help!!
It's driving me crazy!
I created a glossary of wireless terms and I made a sniplet of code to search each article and/or news story as it's being displayed for the user. The issue I'm having now is that it either doesn't replace whole words if there is a keyword that is similar; ex: keywords: 3G & 3GPP & string: The 3GPP Group; will replace 3G and not 3GPP.
I would appreciate any help!!
Code: Select all
// START SEARCHING CURRENT PAGE
if ($nowViewing == "News") {
$query = "SELECT * FROM umtshsdpa_news WHERE whichDB = '".$_GET['newsDB']."' AND id = '".$_GET['artID']."'";
} else {
$query = "SELECT * FROM umtshsdpa_articles WHERE whichDB = '".$_GET['newsDB']."' AND id = '".$_GET['artID']."'";
}
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$artMainContents = $row[3];
$newViewCount = $row[8] + 1;
// END SEARCHING CURRENT PAGE
// START UPDATE PAGE VIEWS & MISC
if ($_COOKIE["umtshsdpa_viewedArticle".$_GET['artID']] != "yes") {
if ($nowViewing == "News") {
$query = "UPDATE umtshsdpa_news SET article_view_count = '$newViewCount' WHERE id = '".$_GET['artID']."'";
} else {
$query = "UPDATE umtshsdpa_articles SET article_view_count = '$newViewCount' WHERE id = '".$_GET['artID']."'";
}
$result = mysql_query($query);
}
$sfinalOver = $row[4] / $row[5]; $finalOver = $sfinalOver / 4;
$pos = strpos($finalOver, ".");
if ($pos) { $finalOverO = substr($finalOver, 0, $pos+2); if (substr($finalOverO, -1, 1) == 0) { $finalOverO = substr($finalOverO, 0, 1); }
} else { $finalOverO = $finalOver; }
// END UPDATE PAGE VIEWS & MISC
// START GET ALL KEYWORDS IN GLOSSARY DB
$keywordsArray = array(); $keywordsArraySmall = array();
$queryA = "SELECT * FROM umtshsdpa_glossary ORDER BY title DESC";
$resultA = mysql_query($queryA) or die("Died: ".mysql_error());
while ($rows = mysql_fetch_object($resultA)) {
if (strlen($rows->title) <= 2) {
$keywordsArraySmall[] = $rows->title;
} else {
$keywordsArray[] = $rows->title;
}
$addKeywords = explode(",", $rows->add_keywords);
for ($y=0; $y<count($addKeywords); $y++) {
if (strlen($addKeywords[$y]) <= 2) {
$keywordsArraySmall[] = $addKeywords[$y];
} else {
$keywordsArray[] = $addKeywords[$y];
}
}
}
// END GET ALL KEYWORDS IN GLOSSARY DB
// START REPLACE ALL KEYWORDS FOUND WITHIN CURRENT PAGE
rsort($keywordsArray);
$contents = explode(" ", $artMainContents);
for ($i=0; $i<count($keywordsArray); $i++) {
if ($keywordsArray[$i] != "") {
$queryB = "SELECT * FROM umtshsdpa_glossary WHERE title='".$keywordsArray[$i]."' OR add_keywords='".$keywordsArray[$i]."' LIMIT 1";
$resultB = mysql_query($queryB) or die("Died: ".mysql_error());
$rowsa = mysql_fetch_row($resultB);
$artMainContents = str_replace("".$keywordsArray[$i]."", "<a href=\"javascript: nullVoid();\" onClick=\"javascript: showTerm('".$rowsa[0]."');\" class=\"uhLinkPlan\">".$keywordsArray[$i]."</a>", $artMainContents);
}
}
rsort($keywordsArraySmall);
$contents = explode(" ", $artMainContents);
for ($i=0; $i<count($keywordsArraySmall); $i++) {
if ($keywordsArraySmall[$i] != "") {
$queryB = "SELECT * FROM umtshsdpa_glossary WHERE title='".$keywordsArraySmall[$i]."' OR add_keywords='".$keywordsArraySmall[$i]."' LIMIT 1";
$resultB = mysql_query($queryB) or die("Died: ".mysql_error());
$rowsa = mysql_fetch_row($resultB);
$artMainContents=preg_replace("/($keywordsArraySmall[$i])/i","<a href=\"javascript: nullVoid();\" onClick=\"javascript: showTerm('".$rowsa[0]."');\" class=\"uhLinkPlan\">\${1}</a>",$artMainContents);
}
}
// END REPLACE ALL KEYWORDS FOUND WITHIN CURRENT PAGE