PHP regular expression to verify whether the input string is the correct IPv6 address or domain name format

PHPz
Release: 2023-06-24 11:44:01
Original
1841 people have browsed it

When writing a website or client application, it is often necessary to verify that the user's input data is in the correct format. One common verification is to verify whether the IP address or domain name format entered by the user meets the standard.

IPv6 is the next generation of IP protocol, which supports more colorful address formats and uses more bits to represent IP addresses. IPv6 addresses have eight groups of 4-digit hexadecimal numbers separated by colons, for example: 2001:0db8:85a3:0000:0000:8a2e:0370:7334. Each set of values ​​in an IPv6 address can be abbreviated to a hexadecimal number with at least one digit or a double colon (::) to represent a zero value.

Domain name is a method of address resolution in the network, which replaces the IP address with a human-readable name. For example, www.google.com is a domain name, and the corresponding IP address is 216.58.194.174.

In PHP, you can use regular expressions to verify whether the input string is in the correct IPv6 address or domain name format. The following is a regular expression in PHP:

IPv6 address:

/^([da-fA-F]{1,4}:){7}[da-fA-F]{1,4}$/i
Copy after login

Domain name:

/^([a-z0-9]+(-[a-z0-9]+)*.)+[a-z]{2,}$/i
Copy after login

The above regular expression can be used to match given characters using the preg_match() function string.

The following code shows how to use the above regular expression to verify IPv6 addresses and domain names:

function is_valid_ipv6($ip) {
  return preg_match('/^([da-fA-F]{1,4}:){7}[da-fA-F]{1,4}$/i', $ip);
}

function is_valid_domain($domain) {
  return preg_match('/^([a-z0-9]+(-[a-z0-9]+)*.)+[a-z]{2,}$/i', $domain);
}

$ipv6 = '2001:0db8:85a3:0000:0000:8a2e:0370:7334';
$domain = 'www.google.com';

if (is_valid_ipv6($ipv6)) {
  echo $ipv6 . '是一个有效的IPv6地址';
} else {
  echo $ipv6 . '不是一个有效的IPv6地址';
}

if (is_valid_domain($domain)) {
  echo $domain . '是一个有效的域名';
} else {
  echo $domain . '不是一个有效的域名';
}
Copy after login

In the above example, we first define the is_valid_ipv6() and is_valid_domain() functions to Verify the format of the IPv6 address and domain name. Then, we defined two strings $ipv6 and $domain, which are an IPv6 address and a domain name respectively. Finally, use an if statement to determine whether the two strings comply with the validation rules.

Summary:

In PHP, you can use regular expressions to verify whether the IPv6 address or domain name format entered by the user is correct. This functionality can be easily implemented in code using the preg_match() function. Writing regular expressions requires some experience, but you can simplify the process with an online regular expression generator.

The above is the detailed content of PHP regular expression to verify whether the input string is the correct IPv6 address or domain name format. 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