Skye
Skye

Reputation: 93

ISO 3779 vehicle VIN decoder in PHP?

Does anyone know of a ISO 3779 vehicle VIN decoder library in PHP, or Perl, or Python (or any other language, I can convert code easily) available as open source/freeware?

Even just something to decode the WMI portion (first 3 positions) would save me a lot of time. Thanks in advance.

http://en.wikipedia.org/wiki/Vehicle_identification_number

Upvotes: 6

Views: 9197

Answers (2)

MichaelB
MichaelB

Reputation: 591

The National Highway Traffic Safety Administration has a database of all the WMI numbers. Here is a link to the relevant web page : NHTSA WMI Data

They reference an ftp site that contains the data: WMI Data Source

You can download the 565.txt file or the mdb database which contains the data. This seems to be a very complete database.

Upvotes: 2

Brad Christie
Brad Christie

Reputation: 101614

I'll let you scrape the data from Wikipedia, but below is a quick (modular) sample you can expand upon (finish off the WMI->Init() method). I would also probably make the VINLookup class a singleton or break off the WMI in to a database (normalize the data somewhere and treat it like a base-33 number like I have [possibly]).

// http://en.wikipedia.org/wiki/Vehicle_identification_number#World_Manufacturer_Identifier

define('VIN_CHARACTERS', 'ABCDEFGHJKLMNPRSTUVWXYZ1234567890'); // no I, O or Q & 0 is last.

class WMI
{
  public $country;
  public $region;
  public $low;
  public $high;

  public function __construct($country,$region,$low,$high)
  {
    $this->country = $country;
    $this->region = $region;
    $this->low = $low;
    $this->high = $high;
  }

  private static function CodeToDec($code)
  {
    if (strlen($code) != 2)
      return false;

    return (strpos(VIN_CHARACTERS, $code{0}) * strlen(VIN_CHARACTERS)) + strpos(VIN_CHARACTERS, $code{1});
  }

  public function IsMatch($vin)
  {
    // first, grab the first 2 characters
    $code = substr($vin,0,2);

    // next, see if it's in range
    // we do this by converting it to a numeric
    $_low = WMI::CodeToDec($this->low);
    $_high = WMI::CodeToDec($this->high);
    $_code = WMI::CodeToDec($code);

    return (($_code >= $_low) && ($_code <= $_high));
  }

  public function __toString()
  {
    return sprintf("%s, %s (%s, %s)", $this->country, $this->region, $this->low, $this->high);
  }
}

class VINLookup
{
  private $vin;
  private $wmis = array();

  public function __construct($vin)
  {
    if (!VINLookup::IsValid($vin))
      throw new Exception('Invalid VIN specified');

    $this->vin = $vin;

    $this->Init();
  }

  private function Init()
  {
    $this->wmis = array(
      new WMI('South Africa',     'Africa',         'AA', 'AH'),
      new WMI('Ivory Coast',      'Africa',         'AJ', 'AN'),
      new WMI('(not assigned)',   'Africa',         'AP', 'A0'),
      new WMI('Angola',           'Africa',         'BA', 'BE'),
      new WMI('Kenya',            'Africa',         'BF', 'BK'),

      new WMI('United States',    'North America',  '1A', '10'),
      new WMI('Canada',           'North America',  '2A', '20'),
      new WMI('Mexico',           'North America',  '3A', '3W'),
      new WMI('Costa Rica',       'North America',  '3X', '37'),
    );
  }

  public function GetCountry()
  {
    foreach ($this->wmis as $wmi)
    {
      if ($wmi->IsMatch($this->vin))
        return $wmi;
    }
    return false;
  }

  public static function IsValid($vin)
  {
    return preg_match('/^[A-HJ-NPR-Z0-9]{17}$/',$vin);
  }
}

Usage:

// check for a valid VIN number supplied
VINLookup::IsValid(<vin>);


// create a new VINLookup with the specified VIN
$lookup = new VINLookup(<vin>);

// retrieve the _Country_ object (above), or FALSE if no country match was found.
$lookup->GetCountry();

Upvotes: 6

Related Questions