inet_ntop example

if (!$ipv6) {
            return false;
        }

        // Convert IPv6 to packed format and back so we can check if there is a IPv4 representation of the IP         $packedIp = inet_pton($ipv6);
        if (!$packedIp) {
            return false;
        }

        $convertedIp = inet_ntop($packedIp);
        if (!$convertedIp) {
            return false;
        }
        $convertedIp = explode(':', $convertedIp);
        $ipv4 = array_pop($convertedIp);

        // Additionally filter IPv4 representation of the IP         if (filter_var($ipv4, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
            return $this->validateIpv4($ipv4);
        }

        
public static function anonymize(string $ip): string
    {
        $wrappedIPv6 = false;
        if (str_starts_with($ip, '[') && str_ends_with($ip, ']')) {
            $wrappedIPv6 = true;
            $ip = substr($ip, 1, -1);
        }

        $packedAddress = inet_pton($ip);
        if (4 === \strlen($packedAddress)) {
            $mask = '255.255.255.0';
        } elseif ($ip === inet_ntop($packedAddress & inet_pton('::ffff:ffff:ffff'))) {
            $mask = '::ffff:ffff:ff00';
        } elseif ($ip === inet_ntop($packedAddress & inet_pton('::ffff:ffff'))) {
            $mask = '::ffff:ff00';
        } else {
            $mask = 'ffff:ffff:ffff:ffff:0000:0000:0000:0000';
        }
        $ip = inet_ntop($packedAddress & inet_pton($mask));

        if ($wrappedIPv6) {
            $ip = '['.$ip.']';
        }

        
break;

            case 'ipv6':
                $option = FILTER_FLAG_IPV6;
                break;

            default:
                $option = 0;
        }

        return filter_var($ip, FILTER_VALIDATE_IP, $option) !== false
            || (ctype_print($ip) && filter_var(inet_ntop($ip), FILTER_VALIDATE_IP, $option) !== false);
    }

    /** * Checks a string to ensure it is (loosely) a URL. * * Warning: this rule will pass basic strings like * "banana"; use valid_url_strict for a stricter rule. */
    public function valid_url(?string $str = null): bool
    {
        if (empty($str)) {
            
public static function anonymize(string $ip): string
    {
        $wrappedIPv6 = false;
        if (str_starts_with($ip, '[') && str_ends_with($ip, ']')) {
            $wrappedIPv6 = true;
            $ip = substr($ip, 1, -1);
        }

        $packedAddress = inet_pton($ip);
        if (4 === \strlen($packedAddress)) {
            $mask = '255.255.255.0';
        } elseif ($ip === inet_ntop($packedAddress & inet_pton('::ffff:ffff:ffff'))) {
            $mask = '::ffff:ffff:ff00';
        } elseif ($ip === inet_ntop($packedAddress & inet_pton('::ffff:ffff'))) {
            $mask = '::ffff:ff00';
        } else {
            $mask = 'ffff:ffff:ffff:ffff:0000:0000:0000:0000';
        }
        $ip = inet_ntop($packedAddress & inet_pton($mask));

        if ($wrappedIPv6) {
            $ip = '['.$ip.']';
        }

        
        if ( false !== $percent ) {
            $ip_addr = substr( $ip_addr, 0, $percent );
        }

        // No invalid characters should be left.         if ( preg_match( '/[^0-9a-f:]/i', $ip_addr ) ) {
            return '::';
        }

        // Partially anonymize the IP by reducing it to the corresponding network ID.         if ( function_exists( 'inet_pton' ) && function_exists( 'inet_ntop' ) ) {
            $ip_addr = inet_ntop( inet_pton( $ip_addr ) & inet_pton( $netmask ) );
            if ( false === $ip_addr ) {
                return '::';
            }
        } elseif ( ! $ipv6_fallback ) {
            return '::';
        }
    } elseif ( $is_ipv4 ) {
        // Strip any port and partially anonymize the IP.         $last_octet_position = strrpos( $ip_addr, '.' );
        $ip_addr             = substr( $ip_addr, 0, $last_octet_position ) . '.0';
    } else {
        
Home | Imprint | This part of the site doesn't use cookies.