Page 1 of 1

Validating urls with regex

Posted: Thu May 14, 2009 7:37 am
by lettie_dude
Hi

Have a regex I have been using for some time to validate urls. However have just come across a problem where it will not valid a certain type of url. Its using eregi and is as follows:

^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~\[\]])*$

This works on most urls I have encountered. For example http://www.abcaddress.de will validate. However this will not:

http://www.abc-address.de/index.php?id= ... 41c3c3e3e2

Initially I thought it was due to the square brackets being used so I added \[\] which had no effect although I suspect it will once the other part is fixed.

Can anyone shed any light on this? Or point me to a regex that will validate any type of url?

Many thanks

Re: Validating urls with regex

Posted: Thu May 14, 2009 7:54 am
by prometheuzz
lettie_dude wrote:Hi

Have a regex I have been using for some time to validate urls. However have just come across a problem where it will not valid a certain type of url. Its using eregi and is as follows:

^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~\[\]])*$

This works on most urls I have encountered. For example http://www.abcaddress.de will validate. However this will not:

http://www.abc-address.de/index.php?id= ... 41c3c3e3e2

Initially I thought it was due to the square brackets being used so I added \[\] which had no effect although I suspect it will once the other part is fixed.

Can anyone shed any light on this? Or point me to a regex that will validate any type of url?

Many thanks
I've never used the ereg library, so I'm not sure if that is the cause of your problems. But with the preg library it works jus fine:

Code: Select all

$text = 'This works on most urls I have encountered. For example http://www.abcaddress.de will validate. However this will not:
http://www.abc-address.de/index.php?id= ... _vdiev_pi1[cmd]=single&user_vdiev_pi1[uid]=428902&cHash=41c3c3e3e2
Initially I thought it was due to the s';
preg_match_all("@(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~\[\]])*@", $text, $matches);
print_r($matches);
Gives the following output:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => http://www.abcaddress.de
            [1] => http://www.abc-address.de/index.php?id= ... _vdiev_pi1[cmd]=single&user_vdiev_pi1[uid]=428902&cHash=41c3c3e3e2
        )
        ...
Not that many of the "normal" regex meta characters don't need escaping inside a character class. The following regex does the same as your original one:

Code: Select all

^(https?|ftp)://[a-zA-Z0-9.-]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?[a-zA-Z0-9._?,'/\\&%$#=~\[\]]*$

Re: Validating urls with regex

Posted: Thu May 14, 2009 8:08 am
by lettie_dude
Cheers prometheuzz preg_match is working for me. Many thanks

Re: Validating urls with regex

Posted: Thu May 14, 2009 8:09 am
by prometheuzz
lettie_dude wrote:Cheers prometheuzz preg_match is working for me. Many thanks
Good to hear it, and you're welcome.