Home  >  Article  >  Backend Development  >  Sample code for php to use preg_match() function to verify IP address

Sample code for php to use preg_match() function to verify IP address

怪我咯
怪我咯Original
2017-07-12 11:50:061529browse

preg_match() Function is used to perform regular expression matching, returning 1 successfully, otherwise returning 0. This article mainly introduces how PHP uses the preg_match() function to verify IP addresses, involving PHP's regular matching operations for numbers and strings. Friends in need can refer to , as follows :

<?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 above is the detailed content of Sample code for php to use preg_match() function to verify 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