输入过滤

//输入过滤 同时去除连续空白字符可参考扩展库的remove_xss
function get_replace_input($str,$rptype=0){
$str = stripslashes($str);
$str = htmlspecialchars($str);
$str = get_replace_nb($str);
return addslashes($str);
}
//去除换行
function get_replace_nr($str){
$str = str_replace(array("<nr/>","<rr/>"),array("\n","\r"),$str);
return trim($str);
}
//去除连续空格
function get_replace_nb($str){
$str = str_replace("&nbsp;",' ',$str);
$str = str_replace(" ",' ',$str);
$str = ereg_replace("[\r\n\t ]{1,}",' ',$str);
return trim($str);
}
//去除所有标准的HTML代码
function get_replace_html($str, $start=0, $length, $charset="utf-8", $suffix=false){
return myubstr(eregi_replace('<[^>]+>','',ereg_replace("[\r\n\t ]{1,}",' ',get_replace_nb($str))),$start,$length,$charset,$suffix);
}
//生成字母前缀
function get_letter($s0){
$firstchar_ord = ord(strtoupper($s0{0}));
if (($firstchar_ord>=65 and $firstchar_ord<=91)or($firstchar_ord>=48 and $firstchar_ord<=57)) return $s0{0};
$s = iconv("UTF-8","gb2312", $s0);
$asc = ord($s{0})*256+ord($s{1})-65536;
if($asc>=-20319 and $asc<=-20284)return "A";
if($asc>=-20283 and $asc<=-19776)return "B";
if($asc>=-19775 and $asc<=-19219)return "C";
if($asc>=-19218 and $asc<=-18711)return "D";
if($asc>=-18710 and $asc<=-18527)return "E";
if($asc>=-18526 and $asc<=-18240)return "F";
if($asc>=-18239 and $asc<=-17923)return "G";
if($asc>=-17922 and $asc<=-17418)return "H";
if($asc>=-17417 and $asc<=-16475)return "J";
if($asc>=-16474 and $asc<=-16213)return "K";
if($asc>=-16212 and $asc<=-15641)return "L";
if($asc>=-15640 and $asc<=-15166)return "M";
if($asc>=-15165 and $asc<=-14923)return "N";
if($asc>=-14922 and $asc<=-14915)return "O";
if($asc>=-14914 and $asc<=-14631)return "P";
if($asc>=-14630 and $asc<=-14150)return "Q";
if($asc>=-14149 and $asc<=-14091)return "R";
if($asc>=-14090 and $asc<=-13319)return "S";
if($asc>=-13318 and $asc<=-12839)return "T";
if($asc>=-12838 and $asc<=-12557)return "W";
if($asc>=-12556 and $asc<=-11848)return "X";
if($asc>=-11847 and $asc<=-11056)return "Y";
if($asc>=-11055 and $asc<=-10247)return "Z";
return 0;
}

/**
* [msubstr 截取字符串长度]
* @param [type] $str [description]
* @param integer $start [description]
* @param [type] $length [description]
* @param string $charset [description]
* @param boolean $suffix [description]
* @return [type] [description]
*/
function myubstr($str, $start=0, $length, $charset="utf-8", $suffix=true){
if(function_exists("mb_substr")){
$slice = mb_substr($str, $start, $length, $charset);
}elseif(function_exists('iconv_substr')) {
$slice = iconv_substr($str,$start,$length,$charset);
if(false === $slice) {
$slice = '';
}
}else{
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("",array_slice($match[0], $start, $length));
}
if(mb_strlen($str,'utf8') > $length){
return $suffix ? $slice.'...' : $slice;
}else{
return $slice;
}
}


/**
* 单向 MD5 加密
*/
function encrypt($password) {
$password = md5($password.C('ENCRYPT_KEY'));
$start = substr($password, 0,8);
$end = substr($password, -8);
$password = $start . $end;
$password = md5($password);
return $password;
}
/**
* 验证手机号
*/
function check_mobile($mobilephone){
//手机号码的正则验证
if(preg_match("/^13[0-9]{1}[0-9]{8}$|15[012356789]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$/",$mobilephone)){
return true;
}else{
return false;
}
}
/**
* 验证QQ
* @param unknown_type $qqstr
*/
function check_qq($qqstr){
$qq_reg = '/^[1-9]{1}[0-9]{4,11}$/';
if (preg_match($qq_reg, $qqstr)){
return true;
}else{
return false;
}
}
/**
* 验证固定电话
*/
function check_tel($tel){
$tel_pattern = '/^(0?(([1-9]\d)|([3-9]\d{2}))-?)?\d{7,8}$/';
if (preg_match($tel_pattern, $tel)){
return true;
}else{
return false;
}
}
/**
* 验证邮箱
*/
function check_email($email){
$chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,5}\$/i";
if (strpos($email, '@') !== false && strpos($email, '.') !== false){
if (preg_match($chars, $email)){
return true;
}else{
return false;
}
}else{
return false;
}
}
/**
* [unique_arr 将二维数组中的重复值去除]
* @param [type] $array2D [description]
* @param boolean $stkeep [description]
* @param boolean $ndformat [description]
* @return [type] [description]
*/
function unique_arr($array2D,$stkeep=false,$ndformat=true){
// 判断是否保留一级数组键 (一级数组键可以为非数字)
if($stkeep) $stArr = array_keys($array2D);
// 判断是否保留二级数组键 (所有二级数组键必须相同)
if($ndformat) $ndArr = array_keys(end($array2D));
//降维,也可以用implode,将一维数组转换为用逗号连接的字符串
foreach ($array2D as $v){
$v = join(",",$v);
$temp[] = $v;
}
//去掉重复的字符串,也就是重复的一维数组
$temp = array_unique($temp);
//再将拆开的数组重新组装
foreach ($temp as $k => $v){
if($stkeep) $k = $stArr[$k];
if($ndformat)
{
$tempArr = explode(",",$v);
foreach($tempArr as $ndkey => $ndval) $output[$k][$ndArr[$ndkey]] = $ndval;
}
else $output[$k] = explode(",",$v);
}
return $output;
}
/**
* [rebuild_array 将二维数组转成一维数组]
* @param [type] $arr [description]
* @return [type] [description]
*/
function rebuild_array($arr){ //rebuild a array
static $tmp=array();
foreach($arr as $key=>$val){
if(is_array($val)){
rebuild_array($val);
}else{
$tmp[] = $val;
}
}
return $tmp;
}
/**
* 获取浏览者ip
* @param unknown_type $default
* @return unknown
*/
function GetRemoteIp($default='127.0.0.1'){
$ip_string = $_SERVER['HTTP_CLIENT_IP'].','.$_SERVER['HTTP_X_FORWARDED_FOR'].','.$_SERVER['REMOTE_ADDR'];
if ( preg_match ("/\d+\.\d+\.\d+\.\d+/", $ip_string, $matches) ) {
return $matches[0];
}
return $default;
}
/**
* 验证身份证号
*/

function isCreditNo($vStr)
{
$vCity = array(
'11','12','13','14','15','21','22',
'23','31','32','33','34','35','36',
'37','41','42','43','44','45','46',
'50','51','52','53','54','61','62',
'63','64','65','71','81','82','91'
);

if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)) return false;

if (!in_array(substr($vStr, 0, 2), $vCity)) return false;

$vStr = preg_replace('/[xX]$/i', 'a', $vStr);
$vLength = strlen($vStr);

if ($vLength == 18)
{
$vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
} else {
$vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
}

if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return false;
if ($vLength == 18)
{
$vSum = 0;

for ($i = 17 ; $i >= 0 ; $i--)
{
$vSubStr = substr($vStr, 17 - $i, 1);
$vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr , 11));
}

if($vSum % 11 != 1) return false;
}

return true;
}
posted @ 2017-09-01 15:17  玲汐  阅读(248)  评论(0编辑  收藏  举报