Link to home
Start Free TrialLog in
Avatar of phenixfilms
phenixfilms

asked on

Dynamically Generate Link From Text

I have a bodycontent form that my users can create. I want to automatically search the body content and turn

www.something.com/this./that/this/

or

www.something.com

or

http://websomething.someting.com

into a link. Help is appreciated

Nathan
ASKER CERTIFIED SOLUTION
Avatar of ldbkutty
ldbkutty
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<?php
   $txt = "http://www.google.com/test";
   $txt = preg_replace( "/(?<!<a href=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i", "<a href=\"\\0\">\\0</a>", $txt );
   echo $txt;
?>
Avatar of cbelle13013
cbelle13013

Assuming that $content is the content you wish to parse through, this will turn the new lines into <BR />, make email links into proper mailto links, and add http:// should the user have forgotten to do that.  


      $content = nl2br($content);
      $str = $content;
       $str = eregi_replace ("[[:alpha:]]+://www", "www",$str);
       $str = ereg_replace ("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/](\.[a-z0-9-]{2,4})+", "<a href=\\0 target=_blank>\\0</a>", $str);
       $str = ereg_replace ("www.[^<>[:space:]]+[[:alnum:]/](\.[a-z0-9-]{2,4})+", "<a href=http://\\0 target=_blank>\\0</a>", $str);
       $str = ereg_replace ("[[:alpha:]]+@[^<>[:space:]]+[[:alnum:]/](\.[a-z0-9-]{2,4})+", "<a href=mailto:\\0 target=_blank>\\0</a>", $str);
      $content = $str;