Skip to content

Instantly share code, notes, and snippets.

@zhanglianxin
Created August 10, 2024 09:38
Show Gist options
  • Save zhanglianxin/ec1ccd654f38cd54dec6bd4812e23a8b to your computer and use it in GitHub Desktop.
Save zhanglianxin/ec1ccd654f38cd54dec6bd4812e23a8b to your computer and use it in GitHub Desktop.
<?php
if (!function_exists('is_valid_ip')) {
// PHP ≥ 8.0
function is_valid_ip(string $ip, ?string $type = null): bool
{
return false !== match ($type) {
'IPv4' => filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4),
'IPv6' => filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6),
default => filter_var($ip, FILTER_VALIDATE_IP),
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment