How to use preg_match() function to verify ip address in php

高洛峰
Release: 2023-03-04 17:22:02
Original
1639 people have browsed it

The example in this article describes how PHP uses the preg_match() function to verify the IP address. Share it with everyone for your reference, the details are 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");
Copy after login

I hope this article will be helpful to everyone in PHP programming.

For more articles related to how PHP uses the preg_match() function to verify the IP address, please pay attention to 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