1.隐藏客户部分手机号码:
$phone = '13812345678';
$preg = "/(1\d{1,2})\d\d(\d{0,2})/";
$re = "\$1****\$3";
echo preg_replace($preg,$re,$phone);
常用正则表达式:
用户名 | /^[a-z0-9_-]{3,16}$/ |
---|---|
密码 | /^[a-z0-9_-]{6,18}$/ |
十六进制值 | /^#?([a-f0-9]{6}|[a-f0-9]{3})$/ |
电子邮箱 | /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/ /^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/ |
URL | /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ |
IP 地址 | /((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)/ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ |
HTML 标签 | /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/ |
删除代码\\注释 | (?<!http:|\S)//.*$ |
2、生成同前缀合同(协议)编号
public function 方法名($Type){
if(!$intoType) return false;
$pre = 'WG';
$signType = $intoType == '1'? 'DY': 'XD';
$max_num = Db::name('cus_sign')->where('ifExternalInput',2)->value('MAX(SUBSTRING(NUMBERS,5))');
if(!$max_num) return $pre.$signType.'000001';
return $pre.$signType.str_pad($max_num+1,6,"0",STR_PAD_LEFT);
}
3、输入框每4位一空格
<input type="text" onkeyup="this.value=this.value.replace(/\D/g,'').replace(/....(?!$)/g,'$& ')" />
4、验证身份证号真实性
function check18IDCard($IDCard){
$IDCard = trim($IDCard);
if(strlen($IDCard) != 18) return 2;
//计算身份证的最后一位验证码 ,根据国家标准GB 11643-1999
$IDCardBody = substr($IDCard, 0, 17); //身份证主体
$IDCardCode = strtoupper(substr($IDCard, 17, 1)); //身份证最后一位的验证码
//加权因子
$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
//校验码对应值
$code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
$checksum = 0;
for ($i = 0; $i < strlen($IDCardBody); $i++) {
$checksum += substr($IDCardBody, $i, 1) * $factor[$i];
}
if ($code[$checksum % 11] == $IDCardCode) {
return 1;
} else {
return 3;
}
}
5、验证银行卡号真实性
function is_bank_card($no){
if (strlen($no) < 16 || strlen($no) > 19) return json_encode(['status' => 0,'msg' => '银行卡号长度必须在16到19之间!']);
if(!preg_match('/^\d*$/', $no)) return json_encode(['status' => 0,'msg' => '银行卡号必须全为数字!']);
/* 验证开头6位 */
$strBin=['10','18','30','35','37','40','41','42','43','44','45','46','47','48','49','50','51','52','53','54','55','56','58','60','62','65','68','69','84','87','88','94','95','98','99'];
if(!in_array(substr($no,0,2),$strBin)) return json_encode(['status' => 0,'msg' => '银行卡号开头6位不符合规范!']);
$last_n = substr($no,strlen($no)-1,1); /*取出最后一位数*/
$bin15to18 = substr($no,0,strlen($no)-1);
$arr_no = str_split($bin15to18); /*前15或18位存进数组*/
$arr_no = array_reverse($arr_no); /*把数组倒序*/
$i = 1;
$total = 0;
foreach ($arr_no as $n) {
if($i%2 != 0) {
/*奇数位*/
$ix = $n * 2;
if($ix >= 10){
$nx = 1 + ($ix % 10);
$total += $nx;
}else{
$total += $ix;
}
} else {
/*偶数位*/
$total += $n;
}
$i++;
}
if($total % 10 == 0) {
$x = 0;
}else{
$x = 10 - ($total % 10);
}
if($x == $last_n){
return json_encode(['status' => 1,'msg' => '输入的银行卡号码合法!']);
} else {
return json_encode(['status' => 0,'msg' => '您输入的银行卡号有误!']);
}
}