Home > php教程 > php手册 > ip地址的正则表达式

ip地址的正则表达式

WBOY
Release: 2016-06-13 10:03:17
Original
1321 people have browsed it

ip地址的正则表达式 下面提供两款关于ip正则表达式的实例,实例做了多个IP的地址的验证,同时把合法的ip地址给打印出来了。

ip地址的正则表达式
下面提供两款关于ip正则表达式的实例,实例做了多个ip的地址的验证,同时把合法的ip地址给打印出来了。
*/
//检查ip地址的正则表达式示例1。
 

$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 是正确的ip地址";
        echo "

";
    }
    else
    {
        echo "$ip 不是正确的ip地址";
        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);
}
 // 检查ip地址的正则表达式示例2。
 $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 是正确的ip地址";
        echo "

";
    }
    else
    {
        echo "$ip 不是正确的ip地址";
        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);
}

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template