0

Possible Duplicate:
PHP validation/regex for URL
How to validate a domain name using Regex & Php?

I currently use this regex pattern to validate domain names before they are added to my database:

$pattern = '/^https?:\/\/([a-zA-Z0-9_\-]+:[^\s@:]+@)?((([a-zA-Z][a-zA-Z0-9\-]+\.)+[a-zA-Z\-]+)|((2(5[0-5]|[0-4][0-9])|[01][0-9]{2}|[0-9]{1,2})\.(2(5[0-5]|[0-4][0-9])|[01][0-9]{2}|[0-9]{1,2})\.(2(5[0-5]|[0-4][0-9])|[01][0-9]{2}|[0-9]{1,2})\.(2(5[0-5]|[0-4][0-9])|[01][0-9]{2}|[0-9]{1,2})))(:[0-9]{1,5})?(\/[!~*\'\(\)a-zA-Z0-9;\/\\\?:\@&=\+\$,%#\._-]*)*$/';

This pattern allows sub domains, ipv4 strings etc. Since this is for a directory, it does not allow strings that are normally valid, such as http://localhost but anyways, what I need to add would be support for ipv6 IP strings and also international domain names like for example müller.com

I currently use:

$url = "http://müller.com/"
if(preg_match($pattern,$url)) {
echo "valid";
} else {
echo "invalid";
}

and it validates pretty much everything I want it to validate, except ipv6 addresses and foreign characters.

I wonder if anyone would be able to come up with the two additions, as it would really make my day. Be aware that I am extremely new to PHP, so please do not throw things at me. Instead, it would be much more useful if you could show me a working example and explain what you have added.

Maybe it's required to convert foreign stuff into a valid punycode URL before checking it and then to allow punycode instead. I am open for suggestions.

Thank you so much - Your help is greatly appreciated

Edit: I doubt this question is a duplicate, as I am not looking for a similar solution, plus the referred existing ones do not work out. FILTER_VALIDATE_URL does not work as it will allow strings that I do not want to allow and other solutions also make my regex pattern useless.

What I am looking for is what I have, but additionally supporting ipv6 IP strings and punycode names / international names.

Community
  • 1
  • 1
Marcus Weller
  • 361
  • 1
  • 3
  • 11
  • 2
    [`FILTER_VALIDATE_URL`](http://www.php.net/manual/en/filter.filters.validate.php) for [`filter_var()`](http://php.net/manual/en/function.filter-var.php) is what you are looking for, I think. – DaveRandom Oct 09 '12 at 16:46
  • I found a very HUGE regex for IPv6 addresses [here](http://forums.intermapper.com/viewtopic.php?t=452). That seems to be an all-cases regex. You can easily find weaker versions of that around Internet if you think you don't need to verify addresses so deeply. Anyway you need to grab one of those regex and put it in your current regex this way: `...{2}|[0-9]{1,2}))|)(:[0-9]{1,5})?...` – Carlos Oct 09 '12 at 17:53

0 Answers0