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

怪我咯
Release: 2023-03-13 17:18:02
Original
1541 people have browsed it

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");
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!