各种正则验证

时间:2022-11-08 18:29:59
$email='fst_-gh@163.com';
//$pattern='^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$';
//$pattern='^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$';
$pattern='^[_.0-9a-z-]+@([0-9a-z]+.)+[a-z]{2,3}$';
 if (eregi($pattern,$email)) {
	echo "OK";
}else{
	echo"err";
} 

 preg_match 邮箱验证方法 (以下三种方法都可以)

$pattern="/([A-Za-z0-9\_]+)\@([\w\-]+)\.([a-z]{2,3})/";
$pattern2="/(^[A-Za-z0-9\_]+)\@([0-9a-z][0-9a-z-]+)\.([a-z]{2,3}$)/";
$pattern3="/([\w\-]+\@[\w\-]+\.[a-z]{2,3}$)/";
if (preg_match($pattern3,$email)) {
  echo "emailok"; 
}

 

echo"<p>难证用户名</p>";
$username='sfdsf432_';
$pattern='/[\w]{6,20}/';	
if(preg_match($pattern,$username)){
	echo"username OK";
}	
echo"<p>验证浮点型</p>";
$number='33.25';
$pattern='/^[1-9]([0-9]*)\.(\d+)/';
if(preg_match($pattern,$number)){
	echo"number ok";
}
echo"<p>验证url</p>";
$url='http://www.163.com/df/?=dsf';
$pattern='/^(http|https):\/\/([\w]+\.)?([\w][\w\-]*)+\.(com|org|net|dk|at|us|tv|info|uk|cn|cc)/';
if(preg_match($pattern,$url)){
	echo"url ok";
}
echo"<p>验证 ip</p>";
$ip='192.168.1.1';
$pattern='/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/';
if(preg_match($pattern,$ip)){
	echo"ip ok";
}
echo"<p>验证 html</p>";
$html="<a href=''>sdf</a>";
$pattern="/^<(.*)>.*<\/(.*)>|<(.*) \/>/";
if(preg_match($pattern,$html)){
	echo"html ok";
}