0

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) )
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
  • Closing a question with one that has been closed itself and has weird answers is not very nice. I apologize for Lawrence Cherone's behavior. But he has earned the right to shut your question down. – KIKO Software Oct 05 '17 at 08:01

1 Answers1

1

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;      
}
KIKO Software
  • 15,283
  • 3
  • 18
  • 33