Results 1 to 9 of 9

Thread: PHP Code: Keyword Links

  1. #1
    Gimme Fries with that!
    Join Date
    Aug 2004
    Posts
    1,469

    PHP Code: Keyword Links

    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.

    PHP Code:
    <?
    $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.

  2. #2
    Working. Masetek's Avatar
    Join Date
    Aug 2005
    Location
    Aust
    Posts
    543
    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.

  3. #3
    Gimme Fries with that!
    Join Date
    Aug 2004
    Posts
    1,469
    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

  4. #4
    Junior Registered
    Join Date
    Jun 2008
    Posts
    4
    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

  5. #5
    Registered
    Join Date
    Jul 2005
    Posts
    33
    str_replace($patterns, $replacements, $string) will be bit faster - you dont need regex patterns for simple macthing

  6. #6
    Junior Registered
    Join Date
    Jun 2008
    Posts
    4
    thanks, I'll give it a try.


  7. #7
    Gimme Fries with that!
    Join Date
    Aug 2004
    Posts
    1,469
    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

  8. #8
    Junior Registered
    Join Date
    Jun 2008
    Posts
    4
    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.

  9. #9
    Junior Registered
    Join Date
    Jun 2008
    Posts
    4
    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!

Similar Threads

  1. Review: Professional PHP Programming
    By Chris in forum Books
    Replies: 6
    Last Post: 07-17-2013, 05:26 AM
  2. PHP Code Snippet: Naughty Word Filter
    By Emancipator in forum Website Programming & Databases
    Replies: 8
    Last Post: 07-28-2009, 11:32 PM
  3. PHP CODE : Word Filter Script Example
    By Emancipator in forum Website Programming & Databases
    Replies: 0
    Last Post: 10-06-2005, 11:57 AM
  4. PHP links script
    By Cutter in forum Website Programming & Databases
    Replies: 6
    Last Post: 11-08-2004, 11:23 PM
  5. Google and links pages
    By Jaffro in forum Search Engine Optimization
    Replies: 3
    Last Post: 10-14-2004, 01:28 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •