Home  >  Article  >  Backend Development  >  Regular expression for ip address_PHP tutorial

Regular expression for ip address_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:58:591283browse

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);
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631361.htmlTechArticleRegular expression of ip address. Two examples of ip regular expression are provided below. The example has multiple IPs. Verify the address and print out the legal IP address at the same time. Regular IP address...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn