Improving regex pattern to also validate ipv6 / punycode / foreign domain names

691 Views Asked by At

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.

0

There are 0 best solutions below