Home > Article > Backend Development > Sample code for php to use preg_match() function to verify IP address
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!