Php regular expression to determine whether it is a valid ID card number, php to determine the ID card number

Source: Internet
Author: User
Tags php regular expression

Php regular expression to determine whether it is a valid ID card number, php to determine the ID card number

This example describes how to use php regular expressions to determine whether a regular ID card number is valid. We will share this with you for your reference. The details are as follows:

/*** Determine whether the ID card number is valid * @ 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 ;}

PS: here we will provide two very convenient Regular Expression tools for your reference:

Javascript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript

Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg

In addition, we provide a local ID card information query tool for your reference:

Online query of local ID card information:
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.