Regular expression of ip address Two examples of IP regular expressions are provided below. The examples verify multiple IP addresses and print out legal IP addresses.
Regular expression of ip address
Two examples of IP regular expressions are provided below. The example verifies multiple IP addresses and prints out the legal IP addresses.
*/
//Regular expression example 1 to check ip address.
$arr_ip = array(
"127.0.0.1",
"218.206.10.123",
"192.221.515.0",
"123.0.0.0.1",
"-12.255.0.10",
"10.9c.132.69",
"255.10.10.255"
);foreach ($arr_ip as $ip)
{
If(validateip($ip))
{
echo "$ip is the correct ip address";
echo "
";
}
else
{
echo "$ip is not the correct ip address";
echo "
";
}
}function validateip($ip)
{
$iparray = explode(".",$ip);
for($i=0;$i{
If($iparray[$i]>255)
return (0);
}
return ereg("[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}",$ ip);
}
//Regular expression example 2 to check ip address.
$arr_ip = array(
"127.0.0.1",
"218.206.10.123",
"192.221.515.0",
"123.0.0.0.1",
"-12.255.0.10",
"10.9c.132.69",
"255.10.10.255"
);foreach ($arr_ip as $ip)
{
If(validateip($ip))
{
echo "$ip is the correct ip address";
echo "
";
}
else
{
echo "$ip is not the correct ip address";
echo "
";
}
}function validateip($ip)
{
$iparray = explode(".",$ip);
for($i=0;$i{
If($iparray[$i]>255)
return (0);
}
return ereg("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$" ,$ip);
}