Solved

Dynamically Generate Link From Text

Posted on 2004-09-28
5
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to hide contact form only if no validation errors 4 62
JSON decode 5 42
Why is my implode function empty 8 39
Sending JSON Via POST In PHP 7 18
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…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
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.

738 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