Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

Adding Links to Keywords

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
brendanwov
Asked:
brendanwov
  • 6
  • 6
  • 4
2 Solutions
 
Random_PeteCommented:
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
 
keteracelCommented:
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
 
keteracelCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
keteracelCommented:
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
 
Random_PeteCommented:
Of course that code won't work - I missplaced the endquote on the "mysql" link ... but good pointers!
0
 
brendanwovAuthor Commented:
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
 
keteracelCommented:
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
 
brendanwovAuthor Commented:
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
 
brendanwovAuthor Commented:
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
 
Random_PeteCommented:
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
 
Random_PeteCommented:
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
 
brendanwovAuthor Commented:
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
 
keteracelCommented:
yes
0
 
keteracelCommented:
the regular expression could be altered to ignore anything within angle brackets though, which would solve this
0
 
brendanwovAuthor Commented:
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
 
brendanwovAuthor Commented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now