PHP匹配当前传入是何种类型

时间:2022-10-17 22:08:38

本文出至:新太潮流网络博客

/**
* [is_string_regular_type 正则自动验证传入数据]
* @E-mial wuliqiang_aa@163.com
* @TIME 2017-08-25
* @WEB http://blog.iinu.com.cn
* @param [string] $keyword [传入的关键字要检索的范围]
* @param [string] $string [需要用正则匹配的内容]
* @return boolean [返回数组状态]
*/
function is_string_regular_type($keyword,$string){
$type_array = array(
//邮箱正则
'email' => [ 'type' => '/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/'],
//用户名
'username' => [ 'type' => '/^\w+((-\w+)|(\.\w+))*[0-9a-zA-Z_]/' , 'number' => '/^\w+((-\w+)|(\.\w+))*[0-9a-zA-Z_]{6,20}/'],
//密码
'password' => [ 'type' => '/^[0-9a-zA-Z_]{6,20}$/']
);
$return_str_success = array(
'email' => ['type'=>'邮箱格式正确'],
'username' => ['type'=>'用户名格式正确','number'=>'用户名长度正确'],
'password' => ['type'=>'密码格式正确']
);
$return_str_error = array(
'email' => ['type'=>'邮箱格式不正确'],
'username' => ['type'=>'用户名格式不正确','number'=>'用户名长度不正确'],
'password' => ['type'=>'密码格式不正确']
);
//用于匹配是否存在该键
foreach($type_array as $k=>$v){
$type_array_if[] = $k;
}
if(in_array($keyword,$type_array_if)){
foreach($type_array[$keyword] as $k=>$v){
//正则比配
$preg_match_chek = preg_match($type_array[$keyword][$k],$string);
}
if($preg_match_chek == 1){
//匹配成功
return array('status'=>$preg_match_chek,'result'=>$return_str_success[$keyword][$k],'message'=>$string);
}else{
//匹配失败
return array('status'=>$preg_match_chek,