PHP function to test if an IP is an IPV6 address

This function will tell you if an IP address is a valid IPV6 address.

function is_ipv6($ip) {
    // If it contains anything other than hex characters, periods, colons or a / it's not IPV6
    if (!preg_match("/^([0-9a-f\.\/:]+)$/",strtolower($ip))) { return false; }

    // An IPV6 address needs at minimum two colons in it
    if (substr_count($ip,":") < 2) { return false; }

    // If any of the "octets" are longer than 4 characters it's not valid
    $part = preg_split("/[:\/]/",$ip);
    foreach ($part as $i) { if (strlen($i) > 4) { return false; } }

    return true;
}
Leave A Reply
All content licensed under the Creative Commons License