Solved

Adding Links to Keywords

Posted on 2004-08-06
16
250 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

831 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