PHPMailer\PHPMailer

PHPMailer::isValidHost()public staticWP 1.0

Validate whether a string contains a valid value to use as a hostname or IP address. IPv6 addresses must include [], e.g. [::1], not just ::1.

Method of the class: PHPMailer{}

No Hooks.

Return

true|false.

Usage

$result = PHPMailer::isValidHost( $host );
$host(string) (required)
The host name or IP address to check

PHPMailer::isValidHost() code WP 6.5.2

public static function isValidHost($host)
{
    //Simple syntax limits
    if (
        empty($host)
        || !is_string($host)
        || strlen($host) > 256
        || !preg_match('/^([a-zA-Z\d.-]*|\[[a-fA-F\d:]+\])$/', $host)
    ) {
        return false;
    }
    //Looks like a bracketed IPv6 address
    if (strlen($host) > 2 && substr($host, 0, 1) === '[' && substr($host, -1, 1) === ']') {
        return filter_var(substr($host, 1, -1), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false;
    }
    //If removing all the dots results in a numeric string, it must be an IPv4 address.
    //Need to check this first because otherwise things like `999.0.0.0` are considered valid host names
    if (is_numeric(str_replace('.', '', $host))) {
        //Is it a valid IPv4 address?
        return filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;
    }
    //Is it a syntactically valid hostname (when embeded in a URL)?
    return filter_var('http://' . $host, FILTER_VALIDATE_URL) !== false;
}