Skip to content

Instantly share code, notes, and snippets.

@tieutantan
Created January 1, 2019 20:52
Show Gist options
  • Save tieutantan/f6ccbe13fd3ad1b5740deba5eefdf576 to your computer and use it in GitHub Desktop.
Save tieutantan/f6ccbe13fd3ad1b5740deba5eefdf576 to your computer and use it in GitHub Desktop.
PHP - Valid IP and CIDR
<?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