Determine whether IMEI or meId is valid

Source: Internet
Author: User

/*-----------------------------------------------
* Determines whether the IMEI or meid represented by this string is legal
* @ Param IMEI
* @ Author helin 2014-08-21 add
* @ Return boolen
* IMEI verification code algorithm:
* (1). multiply the number of even digits by 2 to calculate the sum of single digits and ten digits respectively.
* (2) Add the odd digit number and the value calculated in the previous step.
* (3). If the number of digits obtained is 0, the check digit is 0. Otherwise, the value is 10 minus the single digit.
* For example: 35 89 01 80 69 72 41 even digits multiply by 2 get 5*2 = 10 9*2 = 18 1*2 = 02 0*2 = 00 9*2 = 18 2*2 = 04 1*2 = 02, calculate the sum of odd digits and even digits. 3 + (1 + 0) + 8 + (1 + 8) + 0 + (0 + 2) + 8 + (0 + 0) + 6 + (1 + 8) + 7 + (0 + 4) + 4 + (0 + 2) = 63 => check bits 10-3 = 7
-----------------------------------------------*/

Function is_imei ($ IMEI = '') {If (! $ IMEI) {return false ;}$ imeilen = strlen ($ IMEI); $ deorhex = 10; // initialization. Assume that this string is IMEI :: pure numeric for ($ J = 0; $ j <$ imeilen; $ J ++) {$ c = $ IMEI [$ J]; If (is_numeric ($ C )) {continue;} else if ($ C >='a' & $ C <= 'F ') | ($ C> = 'A' & $ C <= 'F') {$ deorhex = 16 ;} else {// If any character other than the number or hexadecimal number is displayed, return false if it is invalid.} if ($ deorhex = 10 & $ imeilen! = 15) {// All numbers (IMEI), and the length is not 15 return false;} if ($ deorhex = 16) {// contains valid hexadecimal number (meid) if ($ imeilen = 14) {// new rule, 14-bit meid, which is directly legal without verifying return true ;} if ($ imeilen! = 14 & $ imeilen! = 15) {return false; // If the meid length is not 14 or 15, it is determined that it is invalid without verification.} $ COUNT = 0; For ($ I = 0; $ I <7; $ I ++) {$ tmp1 = hexdec ($ IMEI [$ I * 2]); // if it is a hexadecimal character, convert to 10: hexdec ('A') = 10; $ tmp2 = hexdec ($ IMEI [$ I * 2 + 1]) * 2; $ count + = $ tmp1 + floor ($ tmp2/$ deorhex) + $ tmp2 % $ deorhex;} $ endint = $ count % $ deorhex; // The last digit of the number $ paritybit = $ endint> 0? $ Deorhex-$ endint: 0; // check bit return $ paritybit = hexdec ($ IMEI [14]);} $ IMEI = 'a0000020.d46cd'; // '123 '; echo is_imei ($ IMEI )? 1:0; echo '<HR> ';

 

Determine whether IMEI or meId is valid

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.