Solved

Adding Links to Keywords

Posted on 2004-08-06
16
255 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
ID: 11736099
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
ID: 11736310
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
ID: 11736364
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 9

Expert Comment

by:keteracel
ID: 11736393
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
ID: 11736496
Of course that code won't work - I missplaced the endquote on the "mysql" link ... but good pointers!
0
 

Author Comment

by:brendanwov
ID: 11736647
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
ID: 11736702
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
ID: 11736770
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
 

Author Comment

by:brendanwov
ID: 11736857
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
ID: 11736935
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
ID: 11736989
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
ID: 11737014
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
ID: 11737019
yes
0
 
LVL 9

Expert Comment

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

Author Comment

by:brendanwov
ID: 11737252
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
ID: 12190227
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 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