This question already has an answer here:
Example domain: http://мппм.рф
After converting domain into ASCII, it become: Xn--l1aaia.xn--p1ai
But my existing PHP function to valid domain returns false.
Existing function to validate domain
function ValidateDomain($domain)
{
if(!preg_match("/^([-a-z0-9]{2,100})\.([a-z\.]{2,24})$/i", $domain))
{
return false;
}
return $domain;
}
I have tried the following one to validate domain
if( !preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $domain) )
</div>
I think that мппм.рф
is already in UTF8, so forcing a conversion will not help. Your regular expression is quite simple, and can be replace by something like this:
function validateDomain($domain)
{
$parts = explode('.',$domain);
$name = array_shift($parts);
$extension = implode('.',$parts);
if ((strlen($name) >= 2) && (strlen($name) <= 100) &&
(strlen($extension) >= 2) && (strlen($extension) <= 24)) return $domain;
else return FALSE;
}
It will work the same, but also for non-a-z characters, and it is easier to understand than when it uses a regular expression. You can make it slightly more compact and efficient by doing this:
function validateDomain($domain)
{
$parts = explode('.',$domain);
$nameLen = strlen(array_shift($parts));
$extLen = strlen(implode('.',$parts));
if( ($nameLen >= 2) && ($nameLen <= 100) &&
($extLen >= 2) && ($extLen <= 24) ) return $domain;
else return FALSE;
}
You could also use the multibyte string functions like this:
function validateDomain($domain)
{
$point = mb_strpos($domain,'.');
$nameLen = mb_strlen(mb_substr($domain,0,$point));
$extLen = mb_strlen(mb_substr($domain,$point+1));
if( ($nameLen >= 2) && ($nameLen <= 100) &&
($extLen >= 2) && ($extLen <= 24) ) return $domain;
else return FALSE;
}