Click to See Complete Forum and Search --> : make a specific word a link


chrisb
05-22-2007, 12:35 PM
Quick one guys. I've seen on certain sites around where if a specific word comes out of a mysql database ie Dog it makes that word a hyperlink (even if it comes out 22 times) and makes it do to a page on the website ie: www.mywebsitedomianname33.com/dog.php

How do they do this? Tried googling it but to no avail! Any links to pages about this welcome!

hastx
05-22-2007, 01:43 PM
Javascript could do this to pick out keywords embedded in html, but fo PHP purposes...you would have to have you article text in an external file that can be opened and processed. Then you would maintain an array of keywords and pages/links that might be relevant.

As your article text is processed, you search for the keyword strings and replace them with the values in the array.

hastx
05-22-2007, 02:16 PM
Here is an example:

<?
$findme=array("dog"=>"<a href=http://www.dog.com>dog</a>","picture"=>"<a href=http://www.photography.com>picture</a>");

//you could retrieve your article text from an external file like below:
//$articleText=file_get_contents('article.txt');

//but for demo purposes, lets just embed the article text in the script:
$articleText="The dog jumped over the picture. Then the picture was taken of the dog. The dog didn't like getting his picture taken, so he bit the picture taker.";

//Now process the article text:
foreach ($findme as $key => $replace) {
$pos = strpos($articleText, $key); //keep a look out for the characters in the array
if ($pos == true) {
$replaced = str_replace($key, $replace, $articleText);
$articleText = $replaced;
}
}

//Print the result
echo"$articleText";
?>


You could see how if this process was used on very large articles, with a lot of links in the array, on a server getting a lot of hits...it would add some overhead. That is why some people use javascript to do the processing on the keywords on the client side.

And i guess, there are other ways you could do this too that may be better, but for a quick example...

chrisb
05-22-2007, 03:29 PM
Thanks. Thought it would be easier than that and maybe it could have been done with ereg_replace or a some kind of replace function or maybe somehow if I had some kind of table in my db which I could create to achieve this.

I just wondered on some football websites you get names like manchester united and every time that's mentioned it's a link to a page about them.

Oh well!

Thanks

Chris

Crucial
05-23-2007, 04:46 PM
In one system where I stored the content in a single variable before flushing the cache, I ran a simple replace like this

$page_content = str_replace($keyword, "<span style='background-color:yellow;'>$keyword</span>", $page_content);

I don't know how this would have responded to heavy use, but it worked fine for the intranet app I was working on.

NogDog
05-23-2007, 05:04 PM
To deal with differences in capitalization as well as avoiding matches on parts of words (such as matching the first 3 letters of "dogma" when searching for "dog"), you could do something like:

$word = 'dog';
$content = preg_replace('/\b('.$word.')\b/i',
"<a href='http://www.mywebsitedomianname33.com/$word.php'>\\1</a>",
$content);

bokeh
05-24-2007, 03:04 AM
You also need to check that the word is not inside an html element, javascript, css, head section, etc.