Page 1 of 1

Issue Replacing Keywords In Current Page

Posted: Wed Nov 02, 2005 9:17 pm
by bbentp
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!!

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

Posted: Thu Nov 03, 2005 5:33 am
by Chris Corbyn
Ouch...

Pseudo:

Code: Select all

$keywords = array();
$replace = array();

$result = mysql_query($query_to_get_keywords);
while ($row = mysql_fetch_array($result))
{
    $keywords[] = $row[0]; //Add to stack
    $replace[] = '<script blah blah>'.$row[0].'</script>';
}

$query = 'select id, some_contents from some_table where some_condition';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
    $new = stri_replace($keywords, $replace, $row[1]);
    $update_query = 'update some_table set some_contents = \''.mysql_real_escape_string($new).'\' where id = '.$row[0];
    mysql_query($update_query);
}

Coding Issue

Posted: Thu Nov 03, 2005 11:18 am
by bbentp
Thanks for the reply!

The coding is fine it produces the required results except for one thing...

If there is an instance of 2 or more keywords that are similar the shorter keyword defaults to being replaced over the longer keyword..

IE:
String = "Terrestrial Radio Access Network (UTRAN)";
keywords = "UTRA, UTRAN"

The replacement in the document will always be UTRA and not the desired result UTRAN.

Another IE:
String = "In 3rd generation partnership project (3GPP) ";
keywords = "3G, 3GPP"

The replacement in the document will always be 3G and not the desired result 3GPP.

So in essence it's not validating that it's replacing a whole word and not partials of words..

Thanks again for any assistance!

Posted: Thu Nov 03, 2005 11:24 am
by feyd
it doesn't have to validate that it's supposed to be replacing full words, you didn't specify that to it :P

To replace full words

Code: Select all

$text = preg_replace('#\b'.$word.'\b#i',$replacement,$text);

You're the man!

Posted: Thu Nov 03, 2005 11:41 am
by bbentp
Thank you very much feyd!!

I knew that it was missing the full context reg ex expression but couldn't remember it and couldn't find it anywhere!!

Thanks again for the quick response and resolution!

Posted: Thu Nov 03, 2005 12:33 pm
by bbentp
Also one final question if you dont mind..

I'm trying to replace the text with the exact same text that's replacing just with an <a href=.... tag;

I'm trying to use \${1} and/or \\1 to replace the text with the same casing but it's just producing ()..


Any ideas? The code looks like this now:

Code: Select all

$artMainContents = preg_replace('#\b'.$keywordsArray[$i].'\b#i', "<a href=\"javascript: nullVoid();\" onClick=\"javascript: showTerm('".$rowsa[0]."');\" class=\"uhLinkPlan\">".\${1}."</a>", $artMainContents);
Thanks again!

Posted: Thu Nov 03, 2005 12:48 pm
by feyd

Code: Select all

$text = preg_replace('#\b('.preg_quote($word,'#').')\b#i','blah blah \\1',$text);

Posted: Thu Nov 03, 2005 12:55 pm
by bbentp
Thank you so much.... Again!