Solved

Dynamically Generate Link From Text

Posted on 2004-09-28
5
174 Views
Last Modified: 2008-07-03
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
0
Comment
Question by:phenixfilms
  • 2
5 Comments
 
LVL 32

Accepted Solution

by:
ldbkutty earned 50 total points
ID: 12172419
This works for any string that ends with '.com' ...

<?php

$text1 = " Link to Google at google.com <br>";
echo InsertLinks($text1);
 
$text2 = " Link to Google at http://google.com <br>";
echo InsertLinks($text2);

$text3 = " Link to Google at www.google.com <br>";
echo InsertLinks($text3);

$text4 = "Link to Google at http://www.google.com <br>";
echo InsertLinks($text4);

function InsertLinks ( $Text )
{
//  Now match things end with .com
   $NotAnchor = '(?<!"|href=|href\s=\s|href=\s|href\s=)';
   $NotHTTP = '(?<!:\/\/)';
   $Domain = '([\w]+).com';
   $Subdir = '([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?';
   $Expr = '/' . $Subdir .  $Domain . '/i';

  // If http: is found, return as  it is.
   if(preg_match("/\bhttp:/i",$Text))
   return preg_replace( $Expr, "<a href=\"$0\" title=\"$0\" target=\"_blank\">$0</a>", $Text);
   
   // Else, add http:// to the link.
   else
   return preg_replace( $Expr, "<a href=\"http://$0\" title=\"http://$0\" target=\"_blank\">$0</a>", $Text);
 
}

?>

Hope this helps you.
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 12172549
<?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;
?>
0
 
LVL 1

Expert Comment

by:cbelle13013
ID: 12173666
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;
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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 dynamically set the form action using jQuery.

747 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

13 Experts available now in Live!

Get 1:1 Help Now