php正则

于 2024-04-28 发布 阅读量75  

1、判断手机号和座机号

//座机    	
$preg_phone='/^1[345678]\d{9}$/'; 
//手机号
$isTel = "/^([0-9]{3,4}-)?[0-9]{7,8}$/" ;
if(!preg_match($preg_phone,$data['tel']) && !preg_match( $isTel , $data['tel'] )){		   
	exit("<script>alert('手机号码或电话格式错误');history.go(-1)</script>");
}

2、判断邮箱

//邮箱验证 
$email = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
if(!preg_match($email,$data['email'])){		   
	exit("<script>alert('邮箱格式错误');history.go(-1)</script>");
}

3、获取html中的所有图片路径

$test = '<p>444<img height="768" width="1024" alt="" src="/uploadfiles/28/Tree.jpg" /></p>
<p>444<img height="768" width="1024" alt="" src="/uploadfiles/40/Tree.jpg" /></p>';
preg_match_all("/<img.*src=\"([^^]*?)\".*\/>/i",$test,$match);
dump($match);