Regular expressions for IP addresses _php tutorial

Source: Internet
Author: User
Tags ereg
Regular expressions for IP addresses two examples of IP regular expressions are provided below, examples of IP addresses are verified, and legitimate IP addresses are printed.

Regular expressions for IP addresses
Here are two examples of IP regular expressions, examples of IP address authentication, and the legitimate IP address to print out.
*/
Check the IP address of the regular expression Example 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 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);
}
Check the IP address of the regular expression Example 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 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);
}

http://www.bkjia.com/PHPjc/631361.html www.bkjia.com true http://www.bkjia.com/PHPjc/631361.html techarticle Regular expressions for IP addresses two examples of IP regular expressions are provided below, examples of IP addresses are verified, and legitimate IP addresses are printed. The regular of the IP address ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.