php做所有的正则验证 如:身份证 QQ等等

3 篇文章 0 订阅
<?php


/**
  +----------------------------------------------------------
 * 原样输出print_r的内容
  +----------------------------------------------------------
 * @param string    $content   待print_r的内容
  +----------------------------------------------------------
 */
function _out($content, $isEcho = false) {
if($isEcho) echo '<div style="text-align:left">';
echo '<pre>';
print_r($content);
echo '</pre>';
if($isEcho) echo '</div>';
}


/**
  +----------------------------------------------------------
 * 加密密码
  +----------------------------------------------------------
 * @param string    $data   待加密字符串
  +----------------------------------------------------------
 * @return string 返回加密后的字符串
 */
function encrypt($data) {
    return md5(C('AUTH_CODE') . md5($data));
}


/**
 * 检查目标文件夹是否存在,如果不存在则自动创建该目录
 *
 * @access      public
 * @param       string      folder     目录路径。不能使用相对于网站根目录的URL
 *
 * @return      bool
 */
function make_dir($folder) {
    $reval = false;


    if (!file_exists($folder)) {
        /* 如果目录不存在则尝试创建该目录 */
        @umask(0);


        /* 将目录路径拆分成数组 */
        preg_match_all('/([^\/]*)\/?/i', $folder, $atmp);


        /* 如果第一个字符为/则当作物理路径处理 */
        $base = ($atmp[0][0] == '/') ? '/' : '';


        /* 遍历包含路径信息的数组 */
        foreach ($atmp[1] AS $val) {
            if ('' != $val) {
                $base .= $val;


                if ('..' == $val || '.' == $val) {
                    /* 如果目录为.或者..则直接补/继续下一个循环 */
                    $base .= '/';


                    continue;
                }
            }  else {
                continue;
            }


            $base .= '/';
            if (!file_exists($base)) {
                /* 尝试创建目录,如果创建失败则继续循环 */
                if (@mkdir(rtrim($base, '/'), 0777)) {
                    @chmod($base, 0777);
                    $reval = true;
                }
            }
        }
    }
    else {
        /* 路径已经存在。返回该路径是不是一个目录 */
        $reval = is_dir($folder);
    }


    clearstatcache();


    return $reval;
}


/**
 * 验证手机的有效性
 *
 * @param       string      data
 *
 * @return      bool
 */
function check_mobile($data) {
$reg = "/^1[3|4|5|8][0-9]\d{8}$/";

return preg_match($reg, $data);
}


/**
 * 验证身份证的有效性
 *
 * @param       string      data
 *
 * @return      bool
 */
function check_idcard($data) {
$reg = "/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/"; 

return preg_match($reg, $data);



/**
 * 验证邮箱的有效性
 *
 * @param       string      data
 *
 * @return      bool
 */
function check_email($data) {
$reg = "/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/";

return preg_match($reg, $data);
}


/**
 * 检查金额的有效性
 *
 * @param       string      data
 *
 * @return      bool
 */
function check_money($data) {
$data = doubleval($data);

if($data < 0) return false;

return true;
}


/**
 * 获取区域的全称
 *
 * @param       int      regionId
 * @param       int      topId
 *
 * @return      string
 */
function getAddress($regionId, $topId = 1) {
$region = M('region');
$region_name = '';

do {
$region_rlt = $region->where('region_id=' . $regionId)->find();
if($region_rlt) {
$region_name = $region_rlt['region_name'] . $region_name;

if($region_rlt['parent_id'] == $topId) break;
$regionId = $region_rlt['parent_id'];
} else {
break;
}
} while($region_rlt['parent_id'] != '0');

return $region_name;
}


/**
 * 获取区域的所有下级区域编号链
 *
 * @param       int      regionId
 *
 * @return      string
 */
function getRegionIds($regionId) {
$region = M('region');
$region_ids = $regionId;

$region_data = $region->where('parent_id=' . $regionId)->select();
foreach($region_data as $k=>$v) {
$region_ids .= ', ' . getRegionIds($v['region_id']);
}

return $region_ids;
}


/**
 * 产生编号
 *
 * @param:判断编号前面是否允许为0       bool      isZero
 *
 * @return      string
 */
function productSN($isZero = false) {
if($isZero) $new_sn = date('ymd') . rand('100', '999');
else $new_sn = date('ymd') . rand('100000', '999999');

do {
$len = strlen($new_sn);
if($len <= 6) {
$sn .= $new_sn;
break;
}
$deg = rand(0, $len);
$sn .= substr($new_sn,  $deg, 1);
$new_sn = substr($new_sn, 0, $deg) . substr($new_sn, $deg+1);
} while($new_sn);

if($isZero) {
for($i=0; $i<strlen($sn); $i++) if($sn[$i] != '0') break;

if($i != 0) {
$sn = substr($sn, $i);
for($j=0; $j<$i; $j++) {
$start .= '0';
$end .= '9';
}
$start = '1' . substr($start, 1);
$sn .= rand($start, $end);
}
}

return $sn;
}


/**
 * 获取文件
 *
 * @return      array
 */
function getFile($dir) {
//判断路径不为空
if(empty($dir) or !is_string($dir)) return false;
//因编码是UTF8编码,所以将其转换为GB2312编码
$dir = iconv('UTF-8', 'GB2312', $dir);
//判断是否为文件夹
if(!is_dir($dir)) return false;
//打开文件夹
$haddle = opendir($dir);
//打开不成功
if(!$haddle) return false;
while(($file = readdir($haddle)) !== false) {
if($file != '.' and $file != '..') {
if(is_dir($dir . '/' . $file)) {
$files[iconv('GB2312', 'UTF-8', $file)] = getFile($dir . '/' . iconv('GB2312', 'UTF-8', $file));
} else {
$files[] = iconv('GB2312', 'UTF-8', $file);
}
}
}
closedir($haddle);
return $files;
}


/**
 * 创建zip压缩包
 *
 * @return      bool
 */
function create_zip($files = array(), $destination = '', $overwrite = false) {
if(file_exists($destination) && !$overwrite) { return false; }

$valid_files = array();
if(is_array($files)) {
foreach($files as $file) {
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}

if(count($valid_files)) {
$zip = new ZipArchive();
if($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}

foreach($valid_files as $file) {
$zip->addFile($file,$file);
}

$zip->close();
return file_exists($destination);
} else {
return false;
}
}


//发送短信
function get_mobile_code($mobile, $message) {
Vendor('Custom.sms');

$message = iconv('UTF-8', 'GB2312', $message);
$fields = array(
'CorpID' => urlencode(sms::$uid),
'Pwd' => urlencode(sms::$passwd),
'Mobile' => urlencode($mobile),
'Content' => urlencode($message),
'Cell' => '',
'SendTime' => ''
   );
   $result = sms::execPostRequest($fields);
   if($result == 0) $result = 1;
   
   return $result;
}


// 页面跳转
function jump($url, $msg, $isAlert = false) {
header("Content-Type:text/html; charset=utf-8");
echo '<script>';
if($isAlert) echo 'alert("' . $msg . '");';
echo 'window.location.href="' . $url . '"';
echo '</script>';
exit;
}
















//获取IP地址所在地
function address($ip) {
$info = json_decode(file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=' . $ip . '&format=json'), false);
if ($info->ret == 1) {
if($info->country != '中国') return $info->country;

if($info->province == $info->city) return $info->city;

return $info->province . ' ' . $info->city;
} else {
return 'localhost';
}
}


function page($count,$length){//������model��
import("ORG.Util.Page");
$page= new Page($count,$length);
$show=$page->show();
return $show;
}


function getpage(){//������model��
$p=is_numeric((int)$_GET['p'])?(int)$_GET['p']:1;


return $p;
}


?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值