Regular Expression-how to write a regular expression in PHP that matches an intranet IP address ^ 192.168. [0-255] +. [0-255] $

Source: Internet
Author: User
To use a regular expression to match an IP address, write ^ 192.168. [0-255] +. [0-255] $, but it is incorrect! [] [0-9a-z] But not [0-255] Regular Expressions

Use a regular expression to match the IP address.^192\.168\.[0-255]+\.[0-255]$But it is wrong!
Regular Expression[]Not Restricted[0-9a-z]But not limited[0-255]?

Reply content: Regular

Use a regular expression to match the IP address.^192\.168\.[0-255]+\.[0-255]$But it is wrong!
Regular Expression[]Not Restricted[0-9a-z]But not limited[0-255]?

Server
filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);

================ Edit the answer, in fact, I just wanted to introduce this function ================

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

/*** Determine the Intranet IP ** @ param $ ip ** @ returns */public function isInternalIp ($ ip) {$ ip = ip2long ($ ip ); $ net_a = ip2long ('10. 255.255.255 ')> 24; // The Network Address of the reserved ip address of Class A network $ net_ B = ip2long ('2017. 172. 31.255.255 ')> 20; // The Network Address of the reserved ip address for the B-type network $ net_c = ip2long ('1970. 192. 168.20.255 ')> 16; // 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 is in the range you need .}

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[]Only one of them is matched. The length can only be 1.

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

  • The IP address must be used with the subnet mask to determine whether it belongs to the same intranet.
  • The IP address may be expressed as a decimal integer. If the server is installed with an http server, visit http: // 2130706433.
  • IPv6 is also a problem.

Regular expressions should not be used for detailed verification of IP addresses.There are many problems to be solved using regular expressions. For example, the regular expression matches XML/JSON. I am in favor of using regular expressions to roughly find a sub-string that may be an IP address. However, for further verification of the IP address, we recommend that you refer to this topic.

For example, if only IPv4 addresses in general format are used, it is recommended that(?:(\d{1,3})\.)(\d{1,3})And then write the program to check the number of IP addresses.

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

Could use this verification: 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.