Validating urls with regex

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Validating urls with regex

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Validating urls with regex

Post 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._?,'/\\&%$#=~\[\]]*$
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: Validating urls with regex

Post by lettie_dude »

Cheers prometheuzz preg_match is working for me. Many thanks
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Validating urls with regex

Post by prometheuzz »

lettie_dude wrote:Cheers prometheuzz preg_match is working for me. Many thanks
Good to hear it, and you're welcome.
Post Reply