Note that there are some explanatory texts on larger screens.

plurals
  1. POImproving regex pattern to also validate ipv6 / punycode / foreign domain names
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/206059/php-validation-regex-for-url">PHP validation/regex for URL</a><br> <a href="https://stackoverflow.com/questions/3026957/how-to-validate-a-domain-name-using-regex-php">How to validate a domain name using Regex &amp; Php?</a> </p> </blockquote> <p>I currently use this regex pattern to validate domain names before they are added to my database:</p> <pre><code>$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;\/\\\?:\@&amp;=\+\$,%#\._-]*)*$/'; </code></pre> <p>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 <code>http://localhost</code> 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</p> <p>I currently use:</p> <pre><code>$url = "http://müller.com/" if(preg_match($pattern,$url)) { echo "valid"; } else { echo "invalid"; } </code></pre> <p>and it validates pretty much everything I want it to validate, except ipv6 addresses and foreign characters.</p> <p>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.</p> <p>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.</p> <p>Thank you so much - Your help is greatly appreciated</p> <p>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. </p> <p>What I am looking for is what I have, but additionally supporting ipv6 IP strings and punycode names / international names.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload