How to validate input MAC address format using PHP regex

WBOY
Release: 2023-06-24 09:12:01
Original
1526 people have browsed it

In network devices, the MAC address is an important identifier. It consists of 12 hexadecimal numbers, usually separated by colons or dashes, such as: 00:11:22:33:44:55 or 00-11-22-33-44-55. In programming, regular expressions (Regular Expression) can be used to easily verify whether the MAC address format is legal. This article will introduce how to use PHP regular expressions to verify the input MAC address format.

First, we need to understand the basic syntax of regular expressions. In PHP, regular expression matching can be performed using the preg_match() function. The parameter description of this function is as follows:

preg_match(string $pattern, string $subject, array &$matches = null): int

Among them, $pattern represents the regular expression pattern, $subject Indicates the string that needs to be verified. $matches indicates the matching results and can be omitted. This function returns the number of successful matches, and returns 0 if the match fails.

Next, we start writing the regular expression to verify the MAC address format. We need to meet the following conditions:

  1. The MAC address consists of 12 hexadecimal digits
  2. Two adjacent numbers are separated by a colon or a dash
  3. Each number can be an uppercase or lowercase letter A-F or a number 0-9.

According to the above conditions, we can get the regular expression pattern: /^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa -f]{2}$/.

Next, we apply the regular expression pattern to the PHP code for verification. The code is as follows:

$mac_address = "00:11:22:33:44:55";
$pattern = "/^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/";
$result = preg_match($pattern, $mac_address);
if ($result === 1) {
  echo "MAC地址格式合法";
} else {
  echo "MAC地址格式不合法";
}
Copy after login

In the above code, $mac_address represents the MAC address that needs to be verified, $pattern represents the regular expression pattern, and $result represents the matching result. If the match is successful, the $result value is 1 and "MAC address format is legal" is output, otherwise "MAC address format is illegal" is output.

Using the above method, we can easily verify whether the entered MAC address format is legal. In actual projects, we can encapsulate the verification function into an independent tool function to facilitate calls from other codes.

The above is the detailed content of How to validate input MAC address format using PHP regex. For more information, please follow other related articles on the PHP Chinese website!

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