Home  >  Article  >  Backend Development  >  PHP uses the preg_match() function to implement the method of verifying the IP address

PHP uses the preg_match() function to implement the method of verifying the IP address

墨辰丷
墨辰丷Original
2018-05-26 16:36:091999browse

This article mainly introduces the method of PHP using the preg_match() function to verify the IP address, involving PHP's regular matching operations for numbers and strings. Friends in need can refer to the following

Examples of this article Here is how PHP uses the preg_match() function to verify the IP address. Share it with everyone for your reference, the details are as follows:

Code 1, regular implementation

preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ipAddress);

Code 2,

<?php
/*
*@return Boolen
*@param String $ip 要匹配的ip地址
*@param String $pat 匹配的正则规则
*@param Boolen 匹配成功后返回的布尔值
*preg_match()
*0为不成功,1为成功
*/
function fun($ip){
  //0.0.0.0--- 255.255.255.255
  $pat = "/^(((1?\d{1,2})|(2[0-4]\d)|(25[0-5]))\.){3}((1?\d{1,2})|(2[0-4]\d)|(25[0-5]))$/";
  if(preg_match($pat,$ip)){
   $num = preg_match($pat,$ip);
   return $num;
  }else{
   $num = preg_match($pat,$ip);
   return $num;
  }
}
echo fun("255.255.255.255");

The efficiency of the regular code is not as good as the original one, so throw away the link (filter function) and leave.

Filter options, such as filtering private IP addresses, etc.

Usage reference Validating an IP address with PHP's filter_var function

How does php determine whether the IP is a valid IP address

Most people have seen this log, The first impression must be that it is about how to judge through regular expressions.

No, after php5.2.0, there is a special function to make this judgment.

Judge whether it is a legal IP

if(filter_var($ip, FILTER_VALIDATE_IP)) {
// it&#39;s valid
}
else {
// it&#39;s not valid
}

Judge whether it is a legal IPv4 IP address

if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
// it&#39;s valid
}
else {
// it&#39;s not valid
}

Determine whether it is a legal public IPv4 address, private IP addresses such as 192.168.1.1 will be excluded

if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE)) {
// it&#39;s valid
}
else {
// it&#39;s not valid
}

Determine whether it is a legal IPv6 address

if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE)) {
// it&#39;s valid
}
else {
// it&#39;s not valid
}

Determine whether it is a public IPv4 IP or a legal Public IPv6 IP address

if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
// it&#39;s valid
}
else {
// it&#39;s not valid
}

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

php preg_match Matching string length case analysis

PHP preg_matchDetailed explanation of the steps for matching string length

PHP preg_matchImplementing regular expressions Methods for matching functions

The above is the detailed content of PHP uses the preg_match() function to implement the method of verifying the IP address. 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