Page 1 of 1

Regex(pert) Needed

Posted: Thu Aug 01, 2002 3:34 am
by hob_goblin
How would I make a regular expression that checked if input was a valid website?

Like

http://www.blah.com would be valid

but i still want urls like:

http://www.blah.com/foo/
http://www.blah.com/foo
http://www.blah.com/
http://www.blah.com/foo/
http://www.blah.com/foo
http://www.blah.com/
http://www.blah.com
http://foo.blah.com
http://foo.blah.com/

to all work..

how would i do this?
sorry if these seems more like "make me a regexp!" :-/ but they totally confuse me

Posted: Thu Aug 01, 2002 3:41 am
by twigletmac
There's a user comment on the preg_match() man page which has a regex which can be (apparently) used to separate a URL out into its constituent parts (and there's an explanation).

Mac

Posted: Thu Aug 01, 2002 3:52 am
by hob_goblin
didn't work, since everything is "possibly"... i need a function to return false if the input is not a URL

Posted: Thu Aug 01, 2002 3:59 am
by hob_goblin
stole one from phpbb :-)

Code: Select all

if ( !preg_match('#^http:\/\/#i', $site) )
		{
			$site = 'http://' . $site;
		}

		if ( !preg_match('#^http\\:\\/\\/їa-z0-9\-]+\.(їa-z0-9\-]+\.)?їa-z]+#i', $site) )
		{
			$site = '';
		}

Posted: Thu Aug 01, 2002 7:24 am
by llimllib
Just so you know, that is definitely going to require the 'http://'

Posted: Thu Aug 01, 2002 7:28 am
by hob_goblin
did you look at it?

it basically equates out to:

if it has http:// ... next step... if it doesn't, we'll add it, then check the rest

Posted: Thu Aug 01, 2002 7:46 am
by llimllib
haha...good call goblin, it's a bit early in the morning for me...more coffee...

Posted: Thu Aug 01, 2002 11:09 pm
by gnu2php
Why not just do this?

Code: Select all

preg_match('/^(їa-z]+:\/\/|www\.)/i', $input)