Using regexp to replace url with hyperlink.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Using regexp to replace url with hyperlink.

Post by markusn00b »

Now, i've put my hand to this and it just burns me..

How would i go about taking some user input

Code: Select all

 
$_userInput = "http://www.somesite.com is a great site!";
 
and changing it so that it was wrapped in a hyperlink

Code: Select all

 
$_userInput = "<a href=\"http://www.somesite.com\">http://www.somesite.com</a> is a great site!";
 
Any ideas?
Much appreciated.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Using regexp to replace url with hyperlink.

Post by Zoxive »

Code: Select all

$Userinput = 'http://google.com';
 
$Link = "<a href=\"{$Userinput}\">{$Userinput}</a>";
 
$Link2 = '<a href="' . $Userinput . '">' . $Userinput . '</a>';
Unless you are talking about looking for all URLs and making them links.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Using regexp to replace url with hyperlink.

Post by Jonah Bron »

I think that's what he means.

Code: Select all

<?php
$user_input = 'http://www.example.com was a great site!';
$site = strstr($user_input, ' ');
$comment = strstr($user_input, ' ', true);
$output = '<a href="'. $site .'">'. $comment .'</a>';
?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Using regexp to replace url with hyperlink.

Post by Jonah Bron »

Oh! I see. Sorry, ignore the last post.

Code: Select all

<?php
$user_input = 'hi.  I really liked http://www.example.com, and http://example.com !';
 
$user_input = preg_replace('/[a-z0-9-_]*?.((com)|(net))/i', "<a href=\"http://$1\">$1</a>", $user_input);
?>
Untested.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Using regexp to replace url with hyperlink.

Post by Zoxive »

Code: Select all

<?php
 function replaceurl($Matches){
   $Url = $Matches[0];
   if(strstr($Url,'http://')===false){
     $Url = 'http://' . $Url;  
   } 
   return "<a href=\"{$Url}\">$Url</a>";
 }        
   
 $Regex = '((http://)|(www\.))[a-z0-9-_\.]+';
 
 $Input = 'hi there i like www.google.com and use http://gmail.com alot, http://www.google.com';
 
 $Input = preg_replace_callback("#{$Regex}#im",'replaceurl',$Input);
 
 

Code: Select all

string(193) "hi there i like <a href="http://www.google.com">http://www.google.com</a> and use <a href="http://gmail.com">http://gmail.com</a> alot, <a href="http://www.google.com">http://www.google.com</a>"

No where near prefect, but it works.
Post Reply