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

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.