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
Validating urls with regex
Moderator: General Moderators
-
lettie_dude
- Forum Commoner
- Posts: 65
- Joined: Thu Dec 07, 2006 10:10 am
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Validating urls with regex
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: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
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);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
)
...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
Cheers prometheuzz preg_match is working for me. Many thanks
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Validating urls with regex
Good to hear it, and you're welcome.lettie_dude wrote:Cheers prometheuzz preg_match is working for me. Many thanks