Files
tuiguangzhushou/extend/tools/Verify.php
2020-08-06 15:26:41 +08:00

91 lines
2.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情等我喝醉了再说吧 |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace tools;
/**
* 校验工具类
*/
class Verify
{
/**
* 身份证号码校验
* @param string $idcard
* @return boolean
*/
public static function isIdcard($cardNo)
{
if (strlen($cardNo) != 18) {
return false;
}
$idcard_base = substr($cardNo, 0, 17);
$verify_code = substr($cardNo, 17, 1);
$factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
$verify_list = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
$total = 0;
for ($i = 0; $i < 17; $i++) {
$total += substr($idcard_base, $i, 1) * $factor[$i];
}
$mod = $total % 11;
if ($verify_code == $verify_list[$mod]) {
return true;
} else {
return false;
}
}
/**
* 检测手机号码格式
* @param string $mobile
* @return boolean
*/
public static function isMobilePhone($mobile)
{
if (preg_match("/^1[3578]{1}[0-9]{9}$|14[57]{1}[0-9]{8}$/", $mobile)) {
return true;
} else {
return false;
}
}
/**
* JSON校验
* @param string $jsonStr
* @return boolean
*/
public static function isJson($jsonStr)
{
if (!is_object(json_decode($jsonStr))) {
return false;
} else {
return true;
}
}
/**
* 校验日期格式是否合法
* @param [type] $date 日期
* @param string $rule 要校验的格式
* @return boolean
*/
public static function isDate($date, $rule = 'Y-m-d')
{
$unixTime = strtotime($date);
if (!$unixTime || date($rule, $unixTime) != $date) {
return false;
} else {
return true;
}
}
}