Regex(pert) Needed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Regex(pert) Needed

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

didn't work, since everything is "possibly"... i need a function to return false if the input is not a URL
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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 = '';
		}
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

Just so you know, that is definitely going to require the 'http://'
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

haha...good call goblin, it's a bit early in the morning for me...more coffee...
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post by gnu2php »

Why not just do this?

Code: Select all

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