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
Solved

Dynamically Generate Link From Text

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

861 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