php驗證社會安全號碼碼的正確性

來源:互聯網
上載者:User

標籤:str   reg   校正碼   idcard   rto   sub   cti   php   int   

/********************php驗證社會安全號碼碼是否正確函數*********************/
function is_idcard( $id )
{
  $id = strtoupper($id);
  $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
  $arr_split = array();
  if(!preg_match($regx, $id))
  {
    return FALSE;
  }
  if(15==strlen($id)) //檢查15位
  {
    $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
  
    @preg_match($regx, $id, $arr_split);
    //檢查生日日期是否正確
    $dtm_birth = "19".$arr_split[2] . ‘/‘ . $arr_split[3]. ‘/‘ .$arr_split[4];
    if(!strtotime($dtm_birth))
    {
      return FALSE;
    } else {
      return TRUE;
    }
  }
  else      //檢查18位
  {
    $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
    @preg_match($regx, $id, $arr_split);
    $dtm_birth = $arr_split[2] . ‘/‘ . $arr_split[3]. ‘/‘ .$arr_split[4];
    if(!strtotime($dtm_birth)) //檢查生日日期是否正確
    {
      return FALSE;
    }
    else
    {
      //檢驗18位身份證的校正碼是否正確。
      //校正位按照ISO 7064:1983.MOD 11-2的規定產生,X可以認為是數字10。
      $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
      $arr_ch = array(‘1‘, ‘0‘, ‘X‘, ‘9‘, ‘8‘, ‘7‘, ‘6‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘);
      $sign = 0;
      for ( $i = 0; $i < 17; $i++ )
      {
        $b = (int) $id{$i};
        $w = $arr_int[$i];
        $sign += $b * $w;
      }
      $n = $sign % 11;
      $val_num = $arr_ch[$n];
      if ($val_num != substr($id,17, 1))
      {
        return FALSE;
      } //phpfensi.com
      else
      {
        return TRUE;
      }
    }
  }
  
}

 

 

 

調用身份證驗證函式
 $idc=is_idcard("社會安全號碼碼"); 
if($idc){echo "正確";}else{echo "錯誤";}

php驗證社會安全號碼碼的正確性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.