Forum Moderators: coopster

Message Too Old, No Replies

Validate a url in regular expressions

how?

         

Clark

6:16 am on Mar 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've been searching Google and not quite found what I was looking for, even though I thought this would be an easy search.

I want a regular expression that tests if a full url is valid. e.g. of what I'd ideally like:

[somedomain.com...] = good
www.doman.com/filenm = good
somedomain.com/filename =good
gobbledygook = bad

If I can get the test to only answer the first one:
[somedomain.com...] = good

I'll be satisfied with that and program around the lack of http:// in the input.

There is a parse_url function, but that parse the whole thing out rather than just being a test.

BTW, I still hate regular expressions and can't wrap my mind around understanding it.

moltar

6:57 am on Mar 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



^s?https?:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#]+$

Clark

7:15 am on Mar 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks. Sorry for the dumb question but how would the full code look like? Which function do I use?

OK I looked it up, EREGI, correct?

So something like this?


$inputvar="http://www.domain.com/filnemane";
//note the single quotes around your line, is that correct?
if (eregi('^s?https?:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#]+$', $inputvar))
{
echo "'$inputvar' is a valid url";
}
else
{
echo "'$inputvar' is NOT a valid url";
}

moltar

9:08 pm on Mar 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am not sure.. I don't know PHP, I only know regular expressions and Perl ;)

jatar_k

10:35 pm on Mar 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



probably preg_match [php.net]