PHP does not have to verify the true and False ID _php instance

Source: Internet
Author: User
Tags php class strlen

Just look at the code, it's a simple PHP class

Copy Code code as follows:



<?php


$IDCard = new Idcard ();


Var_dump ($IDCard:: Iscard ($_get[' card '));


 


/**


* ID card processing class


 */


class Idcard {


 


//Check ID card is correct


public static function Iscard ($card) {


$card = Self::to18card ($card);


if (strlen ($card)!= 18) {


return false;


        }


 


$cardBase = substr ($card, 0, 17);


 


Return (Self::getverifynum ($cardBase) = = Strtoupper (substr ($card, 17, 1));


    }


 


 


//Format 15-digit ID number is 18-bit


public static function To18card ($card) {


$card = Trim ($card);


 


if (strlen ($card) = = 18) {


return $card;


        }


 


if (strlen ($card)!= 15) {


return false;


        }


 


If the ID card sequence code is 996 997 998 999, these are special codes for centenarians and


if (Array_search substr ($card, 3), Array (' 996 ', ' 997 ', ' 998 ', ' 999 '))!== false) {


$card = substr ($card, 0, 6). ' 18 '. SUBSTR ($card, 6, 9);


} else {


$card = substr ($card, 0, 6). ' 19 '. SUBSTR ($card, 6, 9);


        }


$card = $card. Self::getverifynum ($card);


return $card;


    }


 


//Calculate ID Check code, according to national standard GB 11643-1999


private static function Getverifynum ($cardBase) {


if (strlen ($cardBase)!= 17) {


return false;


        }


//Weighting factor


$factor = Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);


 


//check code corresponding value


$verify _number_list = Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 ');


 


$checksum = 0;


for ($i = 0; $i < strlen ($cardBase); $i + +) {


$checksum + + substr ($cardBase, $i, 1) * $factor [$i];


        }


 


$mod = $checksum% 11;


$verify _number = $verify _number_list[$mod];


 


return $verify _number;


    }


}


?>



In addition, the verification of the identity card number of the site also provides identity card attribution of the query tools are as follows:

Http://tools.jb51.net/bianmin/sfz

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.