Jump to content

Create hyperlink from a url in a message?


lynxus

Recommended Posts

Hi,

 

Currently to display an emoticon in messages using PHP i use:

$message = str_replace(':)','<img align="absbottom" src="http://admin.imsupporting.com/Images/icons/emoticon_smile.png"> </img>',$message);

 

How can i do this with urls?

 

So for instant lets say i had a message: " hello please visit google.com or http://google.com or www.google.com "

 

How can i get my php script to echo those urls as "clickable hyperlinks? "

( Pretty much exactly like how this forum has done it to my message )

 

 

Any help would be great.

Not sure how i can achieve this?

 

Thanks

G

Here's a simple example on an url that would be found in a post..

//First find and add "http://" to an url without..
$URL = "www.example.com";

//Add http://www..
$http = strpos($URL, "http://");
$www = strpos($URL, "http://www.");
if ($http === false){$URL = "http://.". $URL . "";}
elseif ($www === false) {$URL = "http://www.". $URL . "";}


//Replace URL with hyperlink.
$hyperlink = preg_replace('/\\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]/i', '<a href="\\0">\\0</a>', $URL);

www.google.com and google.com will become..

<a href="http://www.google.com">http://www.google.com</a>

 

Hope that helps!

 

Hi, That looks good but taht would only do it for one var?

 

im looking for it to replace "bits" of a var.

 

ie:

 

$message = "Hello my name is bob, please visit www.google.com";

 

It would then look at $message and replace www.google.com with :

 

$message = 'Hello my name is bob, please visit <a href="www.google.com">www.google.com</a>';

 

If that makes sense?

 

Thanks in advance.

G

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.