Detecting whether a url is "clickable"

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Detecting whether a url is "clickable"

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

thanks man :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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;
	}
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

ONION! You're the man~!
Post Reply