文章
位置: 首页 >文章
php常用正则检测验证
- PHP
- 2022-06-14
- 1699
- 0
php常用正则检测验证
//验证是否是合法的身份证号,简单验证
public static function isValidIdCardNo($idcard)
{
$length = strlen($idcard);
//15位老身份证
if ($length == 15)
{
if (checkdate(substr($idcard, 8, 2), substr($idcard, 10, 2), '19' . substr($idcard, 6, 2)))
{
return true;
}
}
//18位二代身份证号
if ($length == 18)
{
if (!checkdate(substr($idcard, 10, 2), substr($idcard, 12, 2), substr($idcard, 6, 4)))
{
return false;
}
$idcard = str_split($idcard);
if (strtolower($idcard[17]) == 'x')
{
$idcard[17] = '10';
}
//加权求和
$sum = 0;
//加权因子
$wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];
for ($i = 0; $i < 17; $i++)
{
$sum += $wi[$i] * $idcard[$i];
}
//得到验证码所位置
$position = $sum % 11;
//身份证验证位值 10代表X
$code = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];
if ($idcard[17] == $code[$position])
{
return true;
}
}
return false;
}
//验证是否是合法的银行卡,不包含信用卡
public static function isValidBankCard($card)
{
if (!is_numeric($card))
{
return false;
}
if (strlen($card) < 16 || strlen($card) > 19)
{
return false;
}
$cardHeader = [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($card, 0, 2), $cardHeader))
{
return false;
}
$numShouldCheck = str_split(substr($card, 0, -1));
krsort($numShouldCheck);
$odd = $odd['gt9'] = $odd['gt9']['tens'] = $odd['gt9']['unit'] = $odd['lt9'] = $even = [];
array_walk($numShouldCheck, function ($item, $key) use (&$odd, &$even, $card){
if ((strlen($card) == 16) && (substr($card, 0, 2) == '62'))
{
$key += 1;
}
if (($key & 1))
{
$t = $item * 2;
if ($t > 9)
{
$odd['gt9']['unit'][] = intval($t % 10);
$odd['gt9']['tens'][] = intval($t / 10);
}
else
{
$odd['lt9'][] = $t;
}
}
else
{
$even[] = $item;
}
});
$total = array_sum($even);
array_walk_recursive($odd, function ($item, $key) use (&$total) {
$total += $item;
});
$luhm = 10 - ($total % 10 == 0 ? 10 : $total % 10);
$lastNumOfCard = substr($card, -1, 1);
if ($luhm != $lastNumOfCard)
{
return false;
}
return true;
}
/**
* 检查是否是AJAX请求。
* Check is ajax request.
*
* @static
* @access public
* @return bool
*/
public static function isAjaxRequest()
{
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') return true;
if(isset($_GET['HTTP_X_REQUESTED_WITH']) && $_GET['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') return true;
return false;
}
/**
* 检查是否是POST请求
*/
public static function isPostRequest()
{
if($_SERVER['REQUEST_METHOD'] == 'POST') return true;
if($_POST) return true;
return false;
}
/**
* 是否是GET提交的
*/
public static function isGetRequest()
{
return $_SERVER['REQUEST_METHOD'] == 'GET' ? true : false;
}
/**
* 获取文本中首张图片地址
* @param [type] $content
* @return [type]
*/
function getfirstpic($content)
{
if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches))
{
$file=$_SERVER['DOCUMENT_ROOT'].$matches[3][0];
if(file_exists($file))
{
return $matches[3][0];
}
}
else
{
return false;
}
}
/**
* 截取中文字符串
* @param string $string 中文字符串
* @param int $sublen 截取长度
* @param int $start 开始长度 默认0
* @param string $code 编码方式 默认UTF-8
* @param string $omitted 末尾省略符 默认...
* @return string
*/
function cut_str($string, $sublen=250, $omitted = '', $start=0, $code='UTF-8')
{
$string = strip_tags($string);
$string = str_replace(" ","",$string);
$string = mb_strcut($string,$start,$sublen,$code);
$string.= $omitted;
return $string;
}
转载:欢迎来到本站,转载请注明文章出处https://www.ormcc.com ,欢迎加入技术讨论群599606903
下一篇:躲开五仁月饼
文章评论
- 0
- 0
- 0
- 分享
- 打赏
ormcc
一个爱捣鼓的程序员
- 160
会员 - 70
今日访问 - 607
文章
IP访问122527次,运行1483天
评论排行
文章归档
- 还没有相关文章