Created
January 1, 2019 20:52
-
-
Save tieutantan/f6ccbe13fd3ad1b5740deba5eefdf576 to your computer and use it in GitHub Desktop.
PHP - Valid IP and CIDR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function valid_ip_or_cidr($input) | |
{ | |
if (is_int(strpos($value, '/'))) | |
{ | |
/* | |
* https://www.regexpal.com/93987 | |
* if ip_cidr valid return 1, else 0 | |
*/ | |
$valid = preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$/', $input); | |
} else | |
{ | |
// if ip valid return ip, else FALSE | |
$valid = filter_var($input, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); | |
} | |
return (boolean)$valid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment