PDA

View Full Version : PHP Code: Keyword Links



Emancipator
05-02-2006, 10:02 AM
Earlier in the year I shared some keyword code but decided I would share some new code with you. Basically what you can do is setup keywords in an array that will be turned into links within the contest of your article. So for example. Lets say you have an article about a fighter named Matt Hughes ( which i do ) and would like the links to other fighters automatically turned into a link. Here is the code that will do it.

This article assumes you store your articles in a CMS system. Here is the code first off and I will then break it down. Keep in mind this is a real world example so you will need to make some changes.



<?
$string = $row_ufc_fighter['article'];
$patterns = array(
'/Royce Gracie/',
'/Kazushi Sakuraba/'
);
$replacements = array
(
'<a href="http://www.ultimate-fighter.ca/ufc-fighter.php?id=5"><FONT COLOR="#CCCCCC">Royce Gracie</font></a>',
'<a href="http://www.ultimate-fighter.ca/ufc-fighter.php?id=11"><FONT COLOR="#CCCCCC">Kazushi Sakuraba</font></a>'
);
echo preg_replace($patterns, $replacements, $string);
?>

$string is the field where the body of your article is stored. $patterns is the keywords I want to replace. $replacements is what I want to replace them with. preg_replace is what does the replacement and actually displays the final code. Basically we are taking two fighter names and replacing them with links to the fighters bio page.

Hopefully this example helps some. I am by no means claiming this is the best or only way it can be done. I am only posting this to help whoever might find it usefull. You can also set it up so that it uses a database instead of manually doing the keywords, links array. Let me know if any questions or any help needed.

Masetek
05-02-2006, 07:48 PM
I am doing a similar thing in a new site I'm working on. I've got a lot of profiles of people and when their name is mentioned in another persons profile (ie Mike Tyson might be mentioned in Lenox Lewis' profile) it links to their profile. The main difference is instead of using an array it gets the info from the db.

Emancipator
05-03-2006, 05:45 AM
yeah so does mine. But this code I figured was a much easier thing for folks to wrap their heads around. Changing this code to get the array varaiables from a DB isnt terribly hard for anyone reading :)

tyb
06-09-2008, 09:13 PM
I'd be interested in picking your brains about this.Very cool stuff. PM me if you'd be able to offer me a little assistance. I will be forever in debt:)

bassplaya
06-10-2008, 02:48 PM
str_replace($patterns, $replacements, $string) will be bit faster - you dont need regex patterns for simple macthing

tyb
06-12-2008, 10:25 AM
thanks, I'll give it a try.

:)

Emancipator
06-12-2008, 01:29 PM
yeah bass is right as i pointed out you can do this 100 different ways. Just shared this code since it was a 2minute project i thought folks might find handy :)

tyb
06-12-2008, 03:18 PM
I'm trying to get this integrated into phpbb3. Can one you you help? I'd be willing to pay for your assistance. I know enough about PHP to change stuff around, but not well enough to know where to paste any code. PM me if you'd be able to help.

thanks in adavnce.

tyb
06-12-2008, 03:20 PM
Emancipator, your post actually gave me some hope that this is possible in a simple way. I had been searching the web for quite some time, without any success. It was an old thread but it came in handly. Thanks!