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.
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.