gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 22:47:50 26 4
gpt4 key购买 nike

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

即使只是解码 WMI 部分(前 3 个位置)也会节省我很多时间。提前致谢。

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

最佳答案

我会让您从维基百科抓取数据,但下面是一个您可以扩展的快速(模块化)示例(完成 WMI->Init() 方法)。我也可能将 VINLookup 类设为单例或将 WMI 分解到数据库中(在某处规范化数据并将其视为 base-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();

关于php - PHP 中的 ISO 3779 车辆 VIN 解码器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6206954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com