PHP: Convert IPv4 to IPv6

PHP: Convert IPv4 to IPv6

The following PHP function will help you easily convert IPv4(IP address version 4) to IPv6(IP address version 6).

The function will return false if the provided IP address is invalid else it will return the equivalent IPv6

function convert_ipv4_to_ipv6($ip)
{
    if (!filter_var($ip, FILTER_VALIDATE_IP)) {
        return false;
    }

    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
        return $ip;
    }

    $bytes = array_map('dechex', explode('.', $ip));

    return vsprintf('0:0:0:0:0:ffff:%02s%02s:%02s%02s', $bytes);
}