Solved

Adding Links to Keywords

Posted on 2004-08-06
16
243 Views
Last Modified: 2008-01-09
LINKS:

http://codedump.nutextonline.com/index.php?id=206&showline=1
http://www.devshed.com/c/a/PHP/PHP-5-A-Sign-that-PHP-Could-Soon-be-Owned-by-Sun

You will see that these online articles have keywords in them like "MySQL", a PHP function name or another technical term.  In the DevShed example, you'll see that when moused over, these keywords pop-up an advertisement.  You'll see in the first example listed that in the PHP code displayed, every function has a link back to it's respective documentation at PHP.NET.

It's obvious to me me that this is not something that the document author did, but rather some post processing method that matches keywords from database results and converts them into links.

Does anyone have a simple example on how to do this, seen a prefab software package that will do this or even know what this methodology is called?

I'd love to be able to do the same thing that my first example does.  Display code on my web site and automatically generate links back to PHP documentation for every function.

Let me know.  Thanks!

Brendan
0
Comment
Question by:brendanwov
  • 6
  • 6
  • 4
16 Comments
 
LVL 1

Expert Comment

by:Random_Pete
Comment Utility
Here's some simple code to do it :

function makelinks($text) {
/*setup links in this array or alternately load it in from a database*/
$links = array(
    "PHP"=>"http://www.downplay.co.uk/webdesign.php",
    "mysql"=>"http://www.downplay.co.uk/"webdesign.php,
    "internet"=>"http://www.downplay.co.uk/webdesign.php",
    "web design"=>"http://www.downplay.co.uk/webdesign.php");

/*go through each link and replace all occurences*/
foreach ($links as $word=>$url) {
    str_replace($word,"<A HREF='$url'>".$word."</A>");
}

}

This will simply replace all the found keys from $links with the specified url.

http://www.downplay.co.uk
PHP Development, Web and Database Design, Nottingham, UK
0
 
LVL 9

Expert Comment

by:keteracel
Comment Utility
problems with the above code may include:

1) replacing partial words i.e. if the keyword is link then the first four letters of linkerage would turn into a link! <-- you'll need a regular expression to deal with this one.
2) would replace words within html tags. <-- this one is difficult to solve. The easiest way would be to not put html code in sections that you want to link keywords!
3) may replace keywords in code you've just added...

I can fix 1 and possibly 3, give me a minute or so...
0
 
LVL 9

Expert Comment

by:keteracel
Comment Utility
this code fixes (1)

function replaceText($keyword, $link, $text) {
         $regexp = "/\b$keyword\b/i";
         $bodytext = preg_replace( $regexp, '<a class="parsedlink" href="$link">$0</a>', $text );
      
         return $bodytext;
   }
0
 
LVL 9

Expert Comment

by:keteracel
Comment Utility
use it like this:

function makelinks($text) {
    /*setup links in this array or alternately load it in from a database*/
    $links = array(
        "PHP"=>"http://www.downplay.co.uk/webdesign.php",
        "mysql"=>"http://www.downplay.co.uk/"webdesign.php,
        "internet"=>"http://www.downplay.co.uk/webdesign.php",
        "web design"=>"http://www.downplay.co.uk/webdesign.php");

    foreach ($links as $word=>$url) {
       replaceText($word, $url, $text);
   }
}
0
 
LVL 1

Expert Comment

by:Random_Pete
Comment Utility
Of course that code won't work - I missplaced the endquote on the "mysql" link ... but good pointers!
0
 

Author Comment

by:brendanwov
Comment Utility
Pete,

Thanks for the code!  Can you provide me a complete, working example?  I'm sort of new to these concepts and tried getting your code working on my own but without any luck.

I don't need MySQL tables, please give me an example with all the information I need in arrays.

Thanks for your patience!

Brendan
0
 
LVL 9

Assisted Solution

by:keteracel
keteracel earned 250 total points
Comment Utility
sorry, a couple of errors...

function makelinks($text) {
    /*setup links in this array or alternately load it in from a database*/
    $links = array(
        "PHP"=>"http://www.downplay.co.uk/webdesign.php",
        "mysql"=>"http://www.downplay.co.uk/webdesign.php",
        "internet"=>"http://www.downplay.co.uk/webdesign.php",
        "web design"=>"http://www.downplay.co.uk/webdesign.php");

    foreach ($links as $word=>$url) {
       $text = replaceText($word, $url, $text);
   }
}

this will suffer from (2) and (3)  though at the moment...
0
 

Author Comment

by:brendanwov
Comment Utility
This is my second comment since Random_Pete's last reply.

Per keteracel's comment about a link being replaced if it contains a keyword, couldn't logic be added where any word that begins with "http://", "https://", "ftp://", "mms://", "rtsp://", etc; contains "www.", ".net", ".com", etc be excluded from link replacement?

That might be an interesting approach to making this code a little more usable.

Let me know.  Thanks!

Brendan
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:brendanwov
Comment Utility
I'll setup another two questions in order to award whoever gets this script in working order 1000pts.  Whoever is the best contributer will be awarded 500pts.  No losers on this one.
0
 
LVL 1

Expert Comment

by:Random_Pete
Comment Utility
preg_replace can take arrays as input so I think this should solve 2...

function makelinks($text) {
    /*setup links in this array or alternately load it in from a database*/
    $links = array(
        "PHP"=>"http://www.downplay.co.uk/webdesign.php",
        "mysql"=>"http://www.downplay.co.uk/webdesign.php",
        "internet"=>"http://www.downplay.co.uk/webdesign.php",
        "web design"=>"http://www.downplay.co.uk/webdesign.php");
    $patterns = array();
    $replaces = array();
    foreach ($links as $word=>$url) {
       $patterns[] = "/\b$keyword\b/i";
       $replaces[] = '<a class="parsedlink" href="'.$link.'">$0</a>'
    }
    $text =  preg_replace( $patterns,$replaces, $text );

}
0
 
LVL 1

Accepted Solution

by:
Random_Pete earned 250 total points
Comment Utility
Hang on.. that's not quite complete ...

function makelinks($text) {
    /*setup links in this array or alternately load it in from a database*/
    $links = array(
        "PHP"=>"http://www.downplay.co.uk/webdesign.php",
        "mysql"=>"http://www.downplay.co.uk/webdesign.php",
        "internet"=>"http://www.downplay.co.uk/webdesign.php",
        "web design"=>"http://www.downplay.co.uk/webdesign.php");
    $patterns = array();
    $replaces = array();
    foreach ($links as $word=>$url) {
       $patterns[] = "/\b$keyword\b/i";
       $replaces[] = '<a class="parsedlink" href="'.$link.'">$0</a>'
    }
    $text =  preg_replace( $patterns,$replaces, $text );

    return $text;
}

And it will fix 3) but not 2) ...
0
 

Author Comment

by:brendanwov
Comment Utility
I intepretted comment #2 as meaning if the following text was included in an article, the link would be messed up.

This is an articke about <a href="http://www.mysql.com/">MySQL</a>.

Output:

This is an articke about <a href="http://www.<a href="http://www.mysql.com/">mysql</a>.com/"><a href="http://www.mysql.com/">MySQL</a></a>.

Am I understanding the problem correctly?

Brendan
0
 
LVL 9

Expert Comment

by:keteracel
Comment Utility
yes
0
 
LVL 9

Expert Comment

by:keteracel
Comment Utility
the regular expression could be altered to ignore anything within angle brackets though, which would solve this
0
 

Author Comment

by:brendanwov
Comment Utility
keteracel or Random_Pete,

Can you post a complete example, starting from "<?php" to "?>"?  I want something that I can begin messing with.

Thanks!

Brendan
0
 

Author Comment

by:brendanwov
Comment Utility
Although we never really came to a resolution on this, I appreciate everyone's efforts!  Equal points are being awarded to keteracel and Random_Pete.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now