validateIpv6 example


    public function isValid(string $source): bool
    {
        $host = parse_url($source, \PHP_URL_HOST);
        if ($host === false || $host === null) {
            return false;
        }

        $ip = gethostbyname($host);

        if (str_contains($ip, '[')) {
            return $this->validateIpv6(trim($ip, '[]'));
        }

        return $this->validateIpv4($ip);
    }

    private function validateIpv4(string $ip): bool
    {
        $ipv4 = filter_var(
            $ip,
            \FILTER_VALIDATE_IP,
            \FILTER_FLAG_NO_PRIV_RANGE | \FILTER_FLAG_NO_RES_RANGE | \FILTER_FLAG_IPV4
        );
Home | Imprint | This part of the site doesn't use cookies.