سؤال

Does anybody know a PHP function for IMEI validation?

هل كانت مفيدة؟

المحلول

IMEI validation uses Luhn check algorithm. I found a link to a page where you can validate your IMEI. Furthermore, at the bottom of this page is a piece of code written in JavaScript to show how to calculate the 15th digit of IMEI and to valid IMEI. I might give you some ideas. You can check it out here http://imei.sms.eu.sk/index.html

نصائح أخرى

Short solution

You can use this (witchcraft!) solution, and simply check the string length:

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;
}

Detailed solution

Here's my original function that explains each step:

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;
}

Maybe can help you :

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

Also this can help you : http://en.wikipedia.org/wiki/IMEI#Check_digit_computation

If i don't misunderstood, IMEI numbers using Luhn algorithm . So you can google this :) Or you can search IMEI algorithm

Maybe your good with the imei validator in the comments here: http://www.php.net/manual/en/function.ctype-digit.php#77718

But I haven't tested it

Check this solution

<?php
function validate_imei($imei)
{
    if (!preg_match('/^[0-9]{15}$/', $imei)) return false;
    $sum = 0;
    for ($i = 0; $i < 14; $i++)
    {
        $num = $imei[$i];
        if (($i % 2) != 0)
        {
            $num = $imei[$i] * 2;
            if ($num > 9)
            {
                $num = (string) $num;
                $num = $num[0] + $num[1];
            }
        }
        $sum += $num;
    }
    if ((($sum + $imei[14]) % 10) != 0) return false;
    return true;
}
$imei = '868932036356090';
var_dump(validate_imei($imei));
?>

Here is a jQuery solution which may be of use: https://github.com/madeinstefano/imei-validator

good fun from kasperhartwich

function validateImei($imei, $use_checksum = true) {
  if (is_string($imei)) {
    if (ereg('^[0-9]{15}$', $imei)) {
      if (!$use_checksum) return true;
      for ($i = 0, $sum = 0; $i < 14; $i++) {
        $tmp = $imei[$i] * (($i%2) + 1 );
        $sum += ($tmp%10) + intval($tmp/10);
      }
      return (((10 - ($sum%10)) %10) == $imei[14]);
    }
  }
  return false;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top