Home  >  Article  >  Backend Development  >  PHP method to implement regular expression to determine whether it is a legal ID number

PHP method to implement regular expression to determine whether it is a legal ID number

墨辰丷
墨辰丷Original
2018-05-24 16:58:361709browse

This article mainly introduces the method of PHP regularity to determine whether it is a legal ID number, involving PHP's regularity for numbers and string operation related skills. Friends in need can refer to the following

This article describes the examples PHP regular method to determine whether it is a legal ID number. Share it with everyone for your reference, the details are as follows:

/**
 * 判断是否为合法的身份证号码
 * @param $mobile
 * @return int
 */
function isCreditNo($vStr){
  $vCity = array(
    '11','12','13','14','15','21','22',
    '23','31','32','33','34','35','36',
    '37','41','42','43','44','45','46',
    '50','51','52','53','54','61','62',
    '63','64','65','71','81','82','91'
  );
  if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)) return false;
  if (!in_array(substr($vStr, 0, 2), $vCity)) return false;
  $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
  $vLength = strlen($vStr);
  if ($vLength == 18) {
    $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
  } else {
    $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
  }
  if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return false;
  if ($vLength == 18) {
    $vSum = 0;
    for ($i = 17 ; $i >= 0 ; $i--) {
      $vSubStr = substr($vStr, 17 - $i, 1);
      $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr , 11));
    }
    if($vSum % 11 != 1) return false;
  }
  return true;
}


The above is the entire content of this article, I hope it will help everyone learn Helps.


Related recommendations:

PHP Check whether the IP is legal

PHP method to check whether the IP address is legal

php verify whether the entered mobile phone number is legal

The above is the detailed content of PHP method to implement regular expression to determine whether it is a legal ID number. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn