标签:
//银行卡算法校验
1 /** 2 * 银行卡号格式检测 3 */ 4 function chkCard($card) 5 { 6 if(empty($card)) return false; 7 8 $cardNo = RSADecode($card); 9 10 //Luhn 算法 11 $cardNo .= ‘‘; 12 $len = strlen($cardNo); 13 if($len != 16 && $len != 19) 14 return false; 15 16 if(!is_numeric($cardNo)) 17 return false; 18 19 $a1 = 0; //偶数和 20 $a2 = 0; //奇数和 21 if($len == 19) { //19位卡 22 for($i = $len - 2; $i >= 0; $i--) { 23 if($i%2 != 0) { 24 $t = $cardNo[$i] * 2; 25 $t .= ‘‘; 26 $a1 += $t[0]; 27 if(isset($t[1])) { 28 $a1 += $t[1]; 29 } 30 } else { 31 $a2 += $cardNo[$i]; 32 } 33 } 34 } 35 if($len == 16) { //16位信用卡 36 for($i = $len - 2; $i >= 0; $i--) { 37 if($i%2 == 0) { 38 $t = $cardNo[$i] * 2; 39 $t .= ‘‘; 40 $a1 += $t[0]; 41 if(isset($t[1])) { 42 $a1 += $t[1]; 43 } 44 } else { 45 $a2 += $cardNo[$i]; 46 } 47 } 48 } 49 50 $mod = ($a2 + $a1) % 10; 51 $last = $mod; 52 if($mod > 0) 53 $last = 10 - $mod; 54 55 if($last != $cardNo[$len - 1]) 56 return false; 57 58 return true; 59 }
/**
* 检测姓名中文格式
*/
1 function chkChinese($name) 2 { 3 //$pattern = ‘/^[\u4E00-\u9FFF]+$/‘; 4 if(preg_match("/^[\x{4e00}-\x{9fa5}]+$/u", $name)) 5 return true; 6 return false; 7 }
1 /** 2 *根据身份证号计算性别 3 **/ 4 function getGender($idNo) 5 { 6 //15位数的身份证最后一位是双数就是女的,单数就是男的 7 //18位数的身份证倒数第二位(第17位)是双数就是女的,单数就是男的 8 $len = strlen($idNo); 9 $gender = ‘先生‘; 10 11 $last = $len == 15 ? substr($idNo, 0, -1) : ($len == 18 ? substr($idNo, -2, 1) : 1); 12 if($last % 2 == 0) 13 $gender = ‘女士‘; 14 return $gender; 15 }
1 /** 2 * 生成指定长度的随机字符串 3 */ 4 function getRandChar($amount) 5 { 6 $str = ‘23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ‘; 7 $len = strlen($str); 8 $charStr = ‘‘; 9 for($i = 0; $i < $amount; $i++) { 10 $rnd = rand(0, $len - 1); 11 $charStr .= $str[$rnd]; 12 } 13 14 return $charStr; 15 }
1 /** 2 * RSA解密 3 */ 4 function RSADecode($argEncoded) 5 { 6 $source = base64_decode($argEncoded); 7 //rsa 配置加载 8 $rsa = configParam(‘RSA‘); 9 $res = openssl_pkey_get_private($rsa[‘privateKey‘], $rsa[‘passPhrase‘]); 10 openssl_private_decrypt($source,$newSource,$res); 11 12 return $newSource; 13 14 }
1 /** 2 * RSA 加密 3 */ 4 function rsaEncode($mob) 5 { 6 $pub = ‘-----BEGIN PUBLIC KEY----- 7 B 8 -----END PUBLIC KEY-----‘; 9 10 $res = openssl_pkey_get_public($pub); 11 openssl_public_encrypt($mob, $newStr, $res); 12 $en = base64_encode($newStr); 13 14 return $en; 15 }
标签:
原文地址:http://www.cnblogs.com/the-moving-ear/p/4184633.html