PHP中常用的几个正则表达式:
经过测试,暂无错误,如有更好的希望告诉我:
EAMIL检测:"/^[/w/d/-_]+@/w+[/w/d/.-_]+/w+$/i"
URL网址检测: "/^(http[s]?:)?([/w-]+/.)+[/w-]+([/w-/.///?/%/&/=]*)?$/i"
身份证号码检测:"/^(/d{15}|/d{17}[/dx]+)+$/i"
英文+数字检测:"/^[a-zA-Z0-9]+$/i"
中文检测:"/^([/xE4-/xE9][/x80-/xBF][/x80-/xBF])+$/"(UTF-8下通过,其它编码没测试)
纯数字检测:"/^/d+$/i"
布尔型检测:"/^(true|false|0|1)+$/i"
SQL表达式检测:"//s*select/s+(top/s+/d+/s+)?(/*|count/((/*|[/w_]+)+/)|[/w_]+)+(/s+as/s+[/w_]+)*(/s*,/s*(/*|[/w_]+)(/s+as/s+[/w_]+)*)*/s+from/s+/[?[/w_]+/]?/s*/i"
完整的PHP函数定义如下:
function istrue($str, $flag){
$flag = strtoupper($flag);
switch($flag){
case "EMAIL":
$pattern = "/^[/w/d/-_]+@/w+[/w/d/.-_]+/w+$/i";
break;
case "URL":
$pattern = "/^(http[s]?:)?([/w-]+/.)+[/w-]+([/w-/.///?/%/&/=]*)?$/i";
break;
case "CID":
$pattern = "/^(/d{15}|/d{17}[/dx]+)+$/i";
break;
case "EN":
$pattern = "/^[a-zA-Z0-9]+$/i";
break;
case "CN":
$pattern = "/^([/xE4-/xE9][/x80-/xBF][/x80-/xBF])+$/";
break;
case "NUM":
$pattern = "/^/d+$/i";
break;
case "BOOL":
$pattern = "/^(true|false|0|1)+$/i";
break;
case "SQL":
$pattern = "//s*select/s+(top/s+/d+/s+)?(/*|count/((/*|[/w_]+)+/)|[/w_]+)+(/s+as/s+[/w_]+)*(/s*,/s*(/*|[/w_]+)(/s+as/s+[/w_]+)*)*/s+from/s+/[?[/w_]+/]?/s*/i";
break;
}
$str = (string)($str);
return preg_match($pattern, $str);
}