src/Hostname.php
private static function myParse(string $hostname, ?array &$domainParts = null, ?string &$tld = null, ?string &$error = null): bool
    {
        if ($hostname === '') {
            $error = 'Hostname "' . $hostname . '" is empty.';

            return false;
        }

        if (strlen($hostname) > 255) {
            $error = 'Hostname "' . $hostname . '" is too long: Maximum allowed length is 255 characters."';

            return false;
        }

        $domainParts = explode(
            '.',
            substr($hostname, -1) === '.' ? substr($hostname, 0, -1) : $hostname // Remove trailing "." from hostname.
        );

        // Is there a top-level domain?
        $tld = count($domainParts) > 1 ? array_pop($domainParts) : null;

        if ($tld !== null) {
            if (!self::myValidateTld($tld, $error)) {
                $error = 'Hostname "' . $hostname . '" is invalid: ' . $error;

                return false;
            }
        }

        // Validate the domain parts.
        if (!self::myValidateDomainParts($domainParts, $error)) {
            $error = 'Hostname "' . $hostname . '" is invalid: ' . $error;

            return false;
        }

        // Normalize result.
        self::myNormalizeDomainParts($domainParts);
        self::myNormalizeTld($tld);

        return true;
    }
private static function myParse(string $hostname, ?array &$domainParts = null, ?string &$tld = null, ?string &$error = null): bool
    {
        if ($hostname === '') {
            $error = 'Hostname "' . $hostname . '" is empty.';

            return false;
        }

        if (strlen($hostname) > 255) {
            $error = 'Hostname "' . $hostname . '" is too long: Maximum allowed length is 255 characters."';

            return false;
        }

        $domainParts = explode(
            '.',
            substr($hostname, -1) === '.' ? substr($hostname, 0, -1) : $hostname // Remove trailing "." from hostname.
        );

        // Is there a top-level domain?
        $tld = count($domainParts) > 1 ? array_pop($domainParts) : null;

        if ($tld !== null) {
            if (!self::myValidateTld($tld, $error)) {
                $error = 'Hostname "' . $hostname . '" is invalid: ' . $error;

                return false;
            }
        }

        // Validate the domain parts.
        if (!self::myValidateDomainParts($domainParts, $error)) {
            $error = 'Hostname "' . $hostname . '" is invalid: ' . $error;

            return false;
        }

        // Normalize result.
        self::myNormalizeDomainParts($domainParts);
        self::myNormalizeTld($tld);

        return true;
    }
private static function myParse ( string $ hostname , ? array & $ domainParts = null , ? string & $ tld = null , ? string & $ error = null ) : bool { if ( $ hostname === '' ) { $ error = 'Hostname "' . $ hostname . '" is empty.' ; return false ; } if ( strlen ( $ hostname ) > 255 ) { $ error = 'Hostname "' . $ hostname . '" is too long: Maximum allowed length is 255 characters."' ; return false ; } $ domainParts = explode ( '.' , substr ( $ hostname , - 1 ) === '.' ? substr ( $ hostname , 0 , - 1 ) : $ hostname // Remove trailing "." from hostname. ) ; // Is there a top-level domain? $ tld = count ( $ domainParts ) > 1 ? array_pop ( $ domainParts ) : null ; if ( $ tld !== null ) { if ( ! self :: myValidateTld ( $ tld , $ error ) ) { $ error = 'Hostname "' . $ hostname . '" is invalid: ' . $ error ; return false ; } } // Validate the domain parts. if ( ! self :: myValidateDomainParts ( $ domainParts , $ error ) ) { $ error = 'Hostname "' . $ hostname . '" is invalid: ' . $ error ; return false ; } // Normalize result. self :: myNormalizeDomainParts ( $ domainParts ) ; self :: myNormalizeTld ( $ tld ) ; return true ; }
Tries to parse a hostname and returns the result or error text.

@param string        $hostname    The hostname.
@param string[]|null $domainParts The domain parts if parsing was successful, undefined otherwise.
@param string|null   $tld         The top-level domain if parsing was successful, undefined otherwise.
@param string|null   $error       The error text if parsing was not successful, undefined otherwise.

@return bool True if parsing was successful, false otherwise.
Tries to parse a hostname and returns the result or error text .