有人知道用于IMEI验证的PHP函数吗?
发布于 2012-01-21 17:47:10
IMEI验证使用Luhn校验算法。我找到一个页面的链接,你可以在那里验证你的IMEI。此外,在此页面的底部是一段用JavaScript编写的代码,用于说明如何计算IMEI的第15位以及如何使IMEI有效。我可能会给你一些建议。你可以在这里查看http://imei.sms.eu.sk/index.html
发布于 2014-07-18 22:20:49
短解
您可以使用this (witchcraft!) solution,只需检查字符串长度:
function is_luhn($n) {
$str = '';
foreach (str_split(strrev((string) $n)) as $i => $d) {
$str .= $i %2 !== 0 ? $d * 2 : $d;
}
return array_sum(str_split($str)) % 10 === 0;
}
function is_imei($n){
return is_luhn($n) && strlen($n) == 15;
}详细解决方案
下面是我的原始函数,它解释了每个步骤:
function is_imei($imei){
// Should be 15 digits
if(strlen($imei) != 15 || !ctype_digit($imei))
return false;
// Get digits
$digits = str_split($imei);
// Remove last digit, and store it
$imei_last = array_pop($digits);
// Create log
$log = array();
// Loop through digits
foreach($digits as $key => $n){
// If key is odd, then count is even
if($key & 1){
// Get double digits
$double = str_split($n * 2);
// Sum double digits
$n = array_sum($double);
}
// Append log
$log[] = $n;
}
// Sum log & multiply by 9
$sum = array_sum($log) * 9;
// Compare the last digit with $imei_last
return substr($sum, -1) == $imei_last;
}发布于 2011-01-20 06:45:17
也许可以帮助你:
This IMEI number is something like this: ABCDEF-GH-IJKLMNO-X (without “-” characters)
For example: 350077523237513
In our example ABCDEF-GH-IJKLMNO-X:
AB is Reporting Body Identifier such as 35 = “British Approvals Board of Telecommunications (BABT)”
ABCDEF is Type Approval Code
GH is Final Assembly Code
IJKLMNO is Serial Number
X is Check Digit这也可以帮助你:http://en.wikipedia.org/wiki/IMEI#Check_digit_computation
如果我没有误解,IMEI是使用Luhn算法的数字。所以你可以google this :)或者你可以搜索IMEI algorithm
https://stackoverflow.com/questions/4741580
复制相似问题