I am just wondering what the best approach for this is. this may not belong in this forum, so I apologize if it doesn't.
What I'm trying to do is take a site url that could be in almost any format (http://www.bla.com, http://www.bla.com, bla.com) and format it into a link. I assume this is probably something I should tackle with regex?
Detecting whether a url is "clickable"
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Definitely regex at the first instance. The pattern for a URL is fairly trivial really. To avoid making things clickable like say a JavaScript object you'll probably need to be restrictive over what's considered valid though. I'd probably only make it clickable if it starts with ^[a-zA-Z]+:// anyway.
I wrote a very basic function to do that sort of thing a while ago...
Code: Select all
function makeLiveLink($value) {
//Links
$value = ereg_replace("(^| )([a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"\\2\" target=\"_BLANK\">\\2</a>", $value);
$value = ereg_replace("(^| )(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\" target=\"_BLANK\">\\2</a>", $value);
//Emails
$value = ereg_replace("(^| )(([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4}))", "\\1<a href=\"mailto:\\2\">\\2</a>", $value);
return $value;
}