Home > Backend Development > PHP Tutorial > PHP regular expression code to determine whether the IP is legal

PHP regular expression code to determine whether the IP is legal

高洛峰
Release: 2016-11-29 16:06:06
Original
1779 people have browsed it

function matchip($q){ 
preg_match('/((25[0-5])|(2[0-4]d)|(1dd)|([1-9]d)|d)(.((25[0-5])|(2[0-4]d)|(1dd)|([1-9]d)|d)){3}/', $q, $matches); 
return $matches[0]; 
} 
$ipaddress = '201.103.2.2'; 
$iperror ='262.3.6.6'; 
$iptest = matchip( $ipaddress ); 
//当我们给matchip 的值为$ipaddress输出为201.103.2.2 
//当我们给matchip的函数值为$iperror时,输出值为 62.3.6.6 
 代码如下 复制代码 
if( $iptest ) 
{ 
 echo $iptest; 
} 
else 
{ 
 echo 'phpfensi.com提示:你输的的ip地址有问题'; 
}
Copy after login


Related labels:
php
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