lynxus Posted August 11, 2009 Share Posted August 11, 2009 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 Quote Link to comment Share on other sites More sharing options...
oni-kun Posted August 11, 2009 Share Posted August 11, 2009 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! Quote Link to comment Share on other sites More sharing options...
lynxus Posted August 11, 2009 Author Share Posted August 11, 2009 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.