4

Does anybody know a PHP function for IMEI validation?

rybo111
  • 12,240
  • 4
  • 61
  • 70
Vladimir Gatev
  • 401
  • 1
  • 5
  • 18

7 Answers7

7

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;
}
rybo111
  • 12,240
  • 4
  • 61
  • 70
1

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

Eray
  • 7,038
  • 16
  • 70
  • 120
1

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

Jasper Briers
  • 555
  • 5
  • 12
  • There are IMEI numbers with 14 15 and 16 digits also there are IMEI numbers containing not only digits – Vladimir Gatev Jan 22 '11 at 15:20
  • @VladimirGatev IMEI numbers are 14 digits + 1 checksum. IMEISV numbers are 14 digits + 2 software version digits. Also, some manufacturers add things like `01` to the end of the IMEI, but this can be ignored in all cases I have seen. – rybo111 Jul 18 '14 at 15:44
  • Casper - please can you post the code that this refers to? – rybo111 Feb 23 '16 at 16:25
1

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));
?>
0

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

rybo111
  • 12,240
  • 4
  • 61
  • 70
szanata
  • 2,402
  • 17
  • 17
0

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;
}
Stremovskyy
  • 452
  • 7
  • 18
0

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

reFORtEM
  • 941
  • 1
  • 9
  • 14