Home > php教程 > PHP源码 > body text

正则验证输入方法集合,带有防注入性质

PHP中文网
Release: 2016-05-25 17:10:35
Original
1236 people have browsed it

php代码

<?php
/*过滤验证方法集合 by joffe 围脖@狂code诗人*/
/* 转义可能引起sql注入的特殊字符.
$string 需要转移的字符串;
$forece 是否强制转换
#string 转后的字符串.
*/
$magic_quotes_gpc = get_magic_quotes_gpc();
function tdsql($string, $force = 0) {
if(!$GLOBALS[&#39;magic_quotes_gpc&#39;] || $force) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = tdsql($val, $force); 
}
} else {
$string = addslashes($string);
}
}
return $string;
}

/*验证是否email
string 需要验证的字符串
# boolen 符合email格式返回true,不符合返回false
*/
function  is_email($string){if (preg_match("/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/",$string)) { return true;} else{return false;}}
/*验证是否url
string 需要验证的字符串是否url
# 是返回true
*/
function is_url($string){if(preg_match("/^[\w-]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$/",$string)){return true;}else{return false;}}

function is_num($str){return is_numeric($str);}
function is_chinese($string){if(preg_match("/[^\x00-\xff]/",$string)){return true;}else{return false;}}
function is_qq($string){if(preg_match(&#39;/^(\d{5,10})$/&#39;,$string)){return true;}else{return false;};}
/*安全字符串,就是只含有英文数字和_*/
function is_safe_str($string){if(preg_match("/^\w+$/",$string)){return true;}else{return false;}}
/*用户名串只有中英文和数字 6到21字节*/
function is_username($string){if(preg_match("/^[\x{4e00}-\x{9fa5}a-zA-Z0-9]{6,21}$/u",$string)){return true;}else{return false;}}
/*密码字符串可以含有部分特殊字符的的 4-18位*/
function is_passwd($string){if(preg_match("/^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,22}$/",$string)){return true;}else{return false;}}
/*中国身份证*/
function is_shenfenzheng($string){if(preg_match("/^(\d{18}|\d{15}|\d{17}x)$/",$string)){return true;}else{return false;}}
/*手机号码 11位简单判断*/
function is_phone($string){if(preg_match("/^\d{11}$/",$string)){return true;}else{return false;}}
/*移动手机号码*/
function is_phone_yd($string){if(preg_match("/^[134-139,147,150-152,157-159,181-183,187-188]\d{9}$/",$string)){return true;}else{return false;}}
/*联通手机号码*/
function is_phone_lt($string){if(preg_match("/^[130-132,155,156,185,186]\d{9}$/",$string)){return true;}else{return false;}}
/*电信手机号码*/
function is_phone_dx($string){if(preg_match("/^[133,153,180,189]\d{9}$/",$string)){return true;}else{return false;}}
/*固定电话*/
function is_telphone($string){if(preg_match("/^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}$/",$string)){return true;}else{return false;}}
/*验证URL 是否来自本站*/
if(isset($_GET[&#39;url&#39;])){if(!is_myurl($_GET[&#39;url&#39;])){$_GET[&#39;url&#39;]="http://".$_SERVER[&#39;HTTP_HOST&#39;]."/";}}
function is_myurl($string){$arr_url = parse_url($string);if($arr_url[&#39;host&#39;] == $_SERVER[&#39;HTTP_HOST&#39;]){return true;}else{return false;}}
/*用户名可以含有中文*/
function is_username_cn($string){if(preg_match("/^(?!_|\s\&#39;)[A-Za-z0-9_\x80-\xff\s\&#39;]{6,27}$/",$string)){return true;}else{return false;};}

?>
Copy after login
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template