Regular expression-how the PHP matches the ^192.168 of the intranet IP address. [0-255]+. [0-255]$

Source: Internet
Author: User

Regular

Want to use a regular to match the IP address, so write ^192\.168\.[0-255]+\.[0-255]$ but is wrong!
Is it [] not possible to limit it [0-9a-z] [0-255] ?

Reply content:

Regular

Want to use a regular to match the IP address, so write ^192\.168\.[0-255]+\.[0-255]$ but is wrong!
Is it [] not possible to limit it [0-9a-z] [0-255] ?

Served the
filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);

============== Edit, the answer is irrelevant, in fact, I would like to introduce this function =============

function isPrivateIP($ip) {    return !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);}

    /**  * 判断内网IP * * @param $ip * * @returns */public function isInternalIp($ip) {    $ip = ip2long($ip);    $net_a = ip2long('10.255.255.255') >> 24; //A类网预留ip的网络地址    $net_b = ip2long('172.31.255.255') >> 20; //B类网预留ip的网络地址    $net_c = ip2long('192.168.255.255') >> 16; //C类网预留ip的网络地址    return $ip >> 24 === $net_a || $ip >> 20 === $net_b || $ip >> 16 === $net_c;}   

$long = ip2long($ip)if ($long == -1 || $long === FALSE) {    echo 'Invalid IP, please try again';}else{    //if $long在你需要的范围即可。}

25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9]

[0-9] Yes, but not [0-10]. Because it [] means only matching one of them. The length can only be 1.

...... I'm curious about your application scenario.

    • The IP address needs to be assisted with the subnet mask to determine whether it belongs to the same intranet.
    • The IP address may be represented as a whole decimal integer. If this machine is equipped with an HTTP server, welcome to visit http://2130706433/, there are surprises.
    • IPV6 is also a problem.

the regular should not be used for detailed authentication of IP addresses. There are a lot of problems with regular to solve is to find the way to death. Just like the regular match Xml/json. I'm in favor of finding a substring of a "possible IP address" with a regular approximation, but for further verification of the IP address, the recommended topic is how to come.

For example, if only the general format of the IPV4 address, in fact, it is recommended (?:(\d{1,3})\.)(\d{1,3}) to match, sift out the IP address of four number after the other program check.

^192\.168\.1?[0-9]{1,2}\.1?[0-9]{1,2}$

Can be verified with this: Http://jex.im/regulex/#!embed=false&flags=&re=%5E (A%7CB) *%3f%24

  • 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.