Home > Backend Development > PHP Tutorial > PHP regular expression to verify validity of IP address

PHP regular expression to verify validity of IP address

王林
Release: 2023-06-24 16:06:02
Original
1261 people have browsed it

When developing web applications, it is often necessary to use IP addresses. The validity of the IP address is very important because if the IP address is invalid, it may lead to security vulnerabilities and other issues. PHP regular expressions are a useful tool that can help us verify the validity of IP addresses.

PHP regular expression is a powerful text processing tool that can be used to match and replace text. In PHP, regular expressions are usually processed using the preg series of functions. To verify the validity of an IP address, we can use the following regular expression:

$pattern = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';
Copy after login

This regular expression can basically match any legal IP address. Let’s explain it in detail below.

First of all, the beginning of the regular expression is "^", which means matching starts from the beginning of the string. Then the part surrounded by "(?:)" is a non-capturing group, which can be matched repeatedly. Next is "(?:25[0-5]|20-4|[01]?0-9?)." This part can match the first three segments of the IP address. The value range of each segment is 0- 255.

Specifically, this part consists of three parts, separated by "|":

  • "25[0-5]" represents a number from 25 to 255.
  • "20-4" means numbers from 200 to 249 and numbers from 250 to 255.
  • "[01]?0-9?" represents a number between 0 and 199. Among them, "[01]?[0-9]" represents the number from 0 to 19, and "[01]?0-9?" represents the number from 0 to 199.

The next step is ".", which means matching "." in the IP address.

The last part is "(?:25[0-5]|20-4|[01]?0-9?)$", this part can match the last segment of the IP address, which is the fourth segment . Its rules are similar to the previous rules and will not be repeated. The final "$" means matching from the end of the string.

It should be noted that this regular expression can only be used to verify the validity of the IPv4 address. If you need to verify the validity of the IPv6 address, you need to use a different regular expression.

The following is a complete PHP code example to verify the validity of the IP address:

function validateIPAddress($ip) {
    $pattern = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';
    if (preg_match($pattern, $ip)) {
        return true;
    } else {
        return false;
    }
}

$ip = '192.168.0.1';
if (validateIPAddress($ip)) {
    echo 'IP地址有效';
} else {
    echo 'IP地址无效';
}
Copy after login

First defines a validateIPAddress function to verify the validity of the IP address. If the IP address is valid, returns true; otherwise returns false. In the function, the regular expression mentioned earlier is used for matching.

Then a $ip variable is defined to store the IP address. If the IP address is valid, "IP address is valid" is output; otherwise, "IP address is invalid" is output.

Through this example, we can see that it is very simple to use PHP regular expressions to verify the validity of an IP address. In practical applications, we can also remove or add some rules as needed to make the verification more accurate.

The above is the detailed content of PHP regular expression to verify validity of IP address. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template