TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Mon Jul 01, 2013 6:56 pm Post subject: Function to check for IPv4 address (contribution) |
|
|
I created a PHP function to check for an IPv4 address and wanted to share it with you all. Enjoy!
Code: | <?php
function is_ipv4($string)
{
// The regular expression checks for any number between 0 and 255 beginning with a dot (repeated 3 times)
// followed by another number between 0 and 255 at the end. The equivalent to an IPv4 address.
return (bool) preg_match('/^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/', $string);
}
// usage
if (is_ipv4('127.0.0.1'))
{
echo 'is a valid IPv4 address';
}
?>
|
Respectfully,
Joshua H. (TRUSTAbyss) |
|