DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Chara has posted 50 posts at DZone. View Full User Profile

Validate IP address in PHP

01.26.2014
| 2630 views |
  • submit to reddit
<?php
$ip_address = '127.0.0.1';
 
if(preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $ip_address)){
   echo 'IP address is valid!';
}else{
   echo 'IP address is NOT valid!';
}
?>

This code demonstrates a simple way to validate IP address using preg_match() PHP function. This is a very simple code, that allows you to perform this task quickly with a minimum lines of code.