src/Parse.php
protected function validateDomainName($domain, $encoding = 'UTF-8')
    {
        if (mb_strlen($domain, $encoding) > 255) {
            return ['valid' => false, 'reason' => 'Domain name too long'];
        } else {
            $origEncoding = mb_regex_encoding();
            mb_regex_encoding($encoding);
            $parts = mb_split('\\.', $domain);
            mb_regex_encoding($origEncoding);
            foreach ($parts as $part) {
                if (mb_strlen($part, $encoding) > 63) {
                    return ['valid' => false, 'reason' => "Domain name part '${part}' too long"];
                }
                if (!preg_match('/^[a-zA-Z0-9\-]+$/', $part)) {
                    return ['valid' => false, 'reason' => "Domain name '${domain}' can only contain letters a through z, numbers 0 through 9 and hyphen.  The part '${part}' contains characters outside of that range."];
                }
                if (mb_substr($part, 0, 1, $encoding) == '-' || mb_substr($part, mb_strlen($part) - 1, 1, $encoding) == '-') {
                    return ['valid' => false, 'reason' => "Parts of the domain name '${domain}' can not start or end with '-'.  This part does: ${part}"];
                }
            }
        }

        // @TODO - possibly check DNS / MX records for domain to make sure it exists?
        return ['valid' => true];
    }
protected function validateDomainName($domain, $encoding = 'UTF-8')
    {
        if (mb_strlen($domain, $encoding) > 255) {
            return ['valid' => false, 'reason' => 'Domain name too long'];
        } else {
            $origEncoding = mb_regex_encoding();
            mb_regex_encoding($encoding);
            $parts = mb_split('\\.', $domain);
            mb_regex_encoding($origEncoding);
            foreach ($parts as $part) {
                if (mb_strlen($part, $encoding) > 63) {
                    return ['valid' => false, 'reason' => "Domain name part '${part}' too long"];
                }
                if (!preg_match('/^[a-zA-Z0-9\-]+$/', $part)) {
                    return ['valid' => false, 'reason' => "Domain name '${domain}' can only contain letters a through z, numbers 0 through 9 and hyphen.  The part '${part}' contains characters outside of that range."];
                }
                if (mb_substr($part, 0, 1, $encoding) == '-' || mb_substr($part, mb_strlen($part) - 1, 1, $encoding) == '-') {
                    return ['valid' => false, 'reason' => "Parts of the domain name '${domain}' can not start or end with '-'.  This part does: ${part}"];
                }
            }
        }

        // @TODO - possibly check DNS / MX records for domain to make sure it exists?
        return ['valid' => true];
    }
protected function validateDomainName ( $ domain , $ encoding = 'UTF-8' ) { if ( mb_strlen ( $ domain , $ encoding ) > 255 ) { return [ 'valid' => false , 'reason' => 'Domain name too long' ] ; } else { $ origEncoding = mb_regex_encoding ( ) ; mb_regex_encoding ( $ encoding ) ; $ parts = mb_split ( '\\.' , $ domain ) ; mb_regex_encoding ( $ origEncoding ) ; foreach ( $ parts as $ part ) { if ( mb_strlen ( $ part , $ encoding ) > 63 ) { return [ 'valid' => false , 'reason' => "Domain name part '${part}' too long" ] ; } if ( ! preg_match ( '/^[a-zA-Z0-9\-]+$/' , $ part ) ) { return [ 'valid' => false , 'reason' => "Domain name '${domain}' can only contain letters a through z, numbers 0 through 9 and hyphen.  The part '${part}' contains characters outside of that range." ] ; } if ( mb_substr ( $ part , 0 , 1 , $ encoding ) == '-' || mb_substr ( $ part , mb_strlen ( $ part ) - 1 , 1 , $ encoding ) == '-' ) { return [ 'valid' => false , 'reason' => "Parts of the domain name '${domain}' can not start or end with '-'.  This part does: ${part}" ] ; } } } // @TODO - possibly check DNS / MX records for domain to make sure it exists? return [ 'valid' => true ] ; }
Determines whether the domain name is valid
@param string $domain The domain name to validate
@param string $encoding The encoding of the string (if not UTF-8)
@return array array('valid' => boolean: whether valid or not,
'reason' => string: if not valid, the reason why);
Determines whether the domain name is valid