John John - 2 months ago 16
PHP Question

PHP simple domain regular expression

I'm trying to figure out why a domain name (example1.commerce) isn't matching the following regular expression:

if (!preg_match('([a-z\d]){1,63}(\.)([a-z\d]){2,16}',$_POST['domain']))
{
echo '<p>Error: domain is not valid.</p>';
}
else {echo '<p>Domain looks good.</p>';}


The pattern should only match the domain (1~63 characters) and domain suffix (2~16 characters); no subdomains, uppercase, etc.

All the online regex testers seem to work though

Answer

'([a-z\d]){1,63}(\.)([a-z\d]){2,16}'

should be

'/([a-z\d]){1,63}(\.)([a-z\d]){2,16}/i'

Most online regex testers add this here for you.