PHP中的ISO 3779车辆VIN解码器?

时间:2011-06-01 20:01:55

标签: php decoder vin

有没有人知道PHP,或Perl或Python(或任何其他语言,我可以轻松转换代码)的ISO 3779车辆VIN解码器库可用作开源/免费软件?

即使是解码WMI部分(前3个位置)的东西也会为我节省大量时间。提前谢谢。

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

2 个答案:

答案 0 :(得分:5)

我会让你从维基百科中删除数据,但下面是一个你可以扩展的快速(模块化)样本(完成WMI->Init()方法)。我也可能会使VINLookup类成为单例或将WMI中断到数据库中(将数据标准化并将其视为基本-33数字,就像我[可能])。

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

用法:

// 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();

答案 1 :(得分:1)

美国国家公路交通安全管理局拥有所有WMI号码的数据库。以下是相关网页的链接:NHTSA WMI Data

他们引用包含数据的ftp网站:WMI Data Source

您可以下载包含数据的565.txt文件或mdb数据库。这似乎是一个非常完整的数据库。