Home  >  Article  >  Backend Development  >  PHP regular expression to verify IP address format

PHP regular expression to verify IP address format

王林
王林Original
2023-06-24 19:11:091049browse

How to use PHP regular expressions to verify IP address format

Using regular expressions to verify IP address format is very important for web developers. IP address is a very common data type in web applications. For example, authentication, logs, network configuration, etc. all require the use of IP addresses. In PHP, using regular expressions to verify IP address format is a very simple and efficient method.

Let’s take a look at how to use regular expressions to verify the IP address format in PHP.

Standards of IP address format

Before verifying the IP address format, we first need to understand the standards of the IP address format. The IP address can be divided into 4 parts, each part has a value between 0 and 255, and each part is separated by a period (.). For example, here are a few valid IP addresses:

  • 192.168.0.1
  • 10.0.0.1
  • 172.16.0.1

And the following is an invalid IP address:

  • 256.0.0.1
  • 192.168.0
  • 192.168.0.1.1

Use Regular expressions for verifying IP address format in PHP

Regular expressions can be used in PHP to verify IP address format. The following is a simple regular expression that can be used to verify the IP address format:

$pattern = '/^((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?)$/';

The meaning of this regular expression is:

  1. ^ represents a string the beginning of.
  2. ((2[0-4]d|25[0-5]|[01]?dd?).) Represents a group of values ​​in an IP address. This group The size of the value is between 0 and 255. The regular expression is expressed as:
  3. 2[0-4]d: When the first digit is 2, the following two digits can It is an integer from 0 to 4 plus any digit.
  4. 25[0-5]: When the first digit is 25, the next two digits can be integers from 0 to 5.
  5. [01]?dd?: When the first digit is 0 or 1, it may be followed by any number from 1 to 2 digits.
  6. . represents a period (.).
  7. {3} means that the previous group value should be repeated 3 times.
  8. (2[0-4]d|25[0-5]|[01]?dd?) represents the last set of values ​​of the IP address, the same as above.
  9. $ represents the end of the string.

Use preg_match() to verify the IP address format

Before using the regular expression, we need to use the PHP built-in function preg_match() to execute the regular expression . The following is an example code that uses preg_match() to verify the IP address format:

function is_valid_ip($ip_address) {
    $pattern = '/^((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?)$/';
    return preg_match($pattern, $ip_address);
}

if (is_valid_ip('192.168.0.1')) {
    echo "有效的IP地址";
} else {
    echo "无效的IP地址";
}

In the above code, we store the regular expression in the variable $pattern , and call the preg_match() function to verify the IP address format. preg_match() will return 1 if the IP address format is valid, 0 otherwise.

Summary

In web development, validating IP address format is a very important task. PHP provides a method to verify the IP address format using regular expressions, which allows us to verify the IP address format quickly and efficiently. Here are a few things to note:

  1. The format of the IP address must comply with the standard specification.
  2. When using regular expressions, remember to escape reserved characters.
  3. Use PHP built-in function preg_match() to execute regular expressions.

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

Statement:
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