Home  >  Article  >  Backend Development  >  How to verify if input is an IPv4 address using regex in PHP

How to verify if input is an IPv4 address using regex in PHP

WBOY
WBOYOriginal
2023-06-24 09:20:341258browse

PHP, as a popular server-side programming language, provides some powerful tools to verify the correctness of input data. In this article, we will focus on how to use regular expressions to verify whether the input is an IPv4 address.

First of all, what is an IPv4 address? An IPv4 address refers to a 32-bit binary number, which is usually divided into four 8-bit binary numbers, separated by ".", and expressed in decimal form. For example, 127.0.0.1 is an IPv4 address.

Now, let’s see how to use regular expressions to verify whether the input is an IPv4 address. The following is a basic regular expression example:

/^(?:[0-9]{1,3}.){3}[0-9]{1,3}$/

This regular expression consists of three parts. The first part is ^, which is used to specify the starting position of the match. The second part is (?:[0-9]{1,3}.){3}, which means matching a combination of three numbers and a dot repeated three times. This combination can match the first three numbers. and dots. (?:) indicates a non-capturing group, that is, it will not be recorded when matching. The last part is [0-9]{1,3}$, which means matching the last number, no other characters after it, and ending with this number.

However, there are still some problems with this regular expression. It allows matching some impossible IPv4 addresses, such as the broadcast address of 256.0.0.0 or 192.168.1.0/24.

In order to solve this problem, we need to add some logic to verify whether each number is between 0~255. The following is an example of a regular expression for verifying an IPv4 address:

/^((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]?)$/

This regular expression also includes three parts. The first part is (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)., which means matching A number starting with 25, starting with 24 and the second number is between 0 and 9, or starting with 0 and 9, in all three cases followed by a dot. This combination will match the first three numbers and the dot.

The second part is the first part repeated three times. The last part is (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?), which means matching Numbers starting with 25, starting with 24 and the second number is between 0 and 9, or starting with 0 and 9. This combination will match the last number.

This regular expression can verify the validity of an IPv4 address, capture each number accurately, and ensure that each number is between 0 and 255.

Here is a sample code that demonstrates how to use this regular expression to check an IP address:

function isIPv4($ip) {
  // 强制类型转换确保$ip为字符串类型
  $ip = (string)$ip;
  
  // 匹配IPv4地址
  $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]?)$/';
  
  return preg_match($pattern, $ip);
}

// 测试验证
var_dump(isIPv4('127.0.0.1')); // true
var_dump(isIPv4('256.0.0.0')); // false

In the above example, we defined a isIPv4() Function to verify an IPv4 address. We cast the IP address to a string type and match it against a regular expression, returning true if the match is successful, otherwise false.

In practical applications, it is very important to verify the data entered by the user. Using regular expressions to validate IPv4 addresses is a good choice for a quick and easy way to check the correctness of user input.

The above is the detailed content of How to verify if input is an IPv4 address using regex in PHP. 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