Validate an IP Address using PHP and Regular Expressions

This php function Validate an IP Address using PHP and Regular Expressions, returns 1 if the IP is valid or 0 if is invalid. The regular expression rather ugly, but still functional and can be very useful. Enjoy it!

 
function valid_ip($ip) {
    return preg_match("/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" .
            "(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $ip);
}
 
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
Leave a comment

0 Comments.

Leave a Reply


[ Ctrl + Enter ]