Solved

Dynamically Generate Link From Text

Posted on 2004-09-28
5
175 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create a Select Query and Populate a Table 3 44
php call to a non-object 3 32
Form not operating correctly. 1 23
How to convert my query to the proper format? 5 17
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 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

25 Experts available now in Live!

Get 1:1 Help Now