Page 1 of 1

Detecting whether a url is "clickable"

Posted: Tue Oct 31, 2006 12:02 pm
by Luke
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?

Posted: Tue Oct 31, 2006 12:08 pm
by Chris Corbyn
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.

Posted: Tue Oct 31, 2006 12:26 pm
by Luke
thanks man :)

Posted: Tue Oct 31, 2006 3:54 pm
by onion2k
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;
	}

Posted: Tue Oct 31, 2006 3:58 pm
by Luke
ONION! You're the man~!