/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
To use this expression follow the code below.
//Save the expression in a variable.
$correct_ip_format = "/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/";
//some input from a user through a text field or from a server.
$user_input = $_REQUEST['ip_address'];
//match the two input's and save it in a variable.
ip_match = preg_match($correct_ip_format, $user_input);
//Condition goes here.
if(ip_match){
echo "Correct IP Address";
}
else{
echo "Incorrect IP Address";
}