Function to check for IPv4 address (contribution)

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3752
Location: USA, GA

PostPosted: Mon Jul 01, 2013 6:56 pm    Post subject: Function to check for IPv4 address (contribution) Reply with quote

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)
Back to top View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group