PHP VIN编号验证代码

PHP VIN编号验证代码,php,validation,vin,Php,Validation,Vin,是否有人知道任何用PHP编写的车辆识别号()验证代码?我只需要检查输入的vin编号是否正确?它是“vin”。“vin编号”=“车辆识别号”,这没有意义 您可以在此处看到VIN结构的定义: 您可以从那里开始工作,也可以在这里获取此脚本: 以下是jordan发布的功能的改进版本: $vin = "1M8GDM9AXKP042788"; function validate_vin($vin) { $vin = strtolower($vin); if (!preg_match

是否有人知道任何用PHP编写的
车辆识别号
()验证代码?我只需要检查输入的vin编号是否正确?

它是“vin”。“vin编号”=“车辆识别号”,这没有意义

您可以在此处看到VIN结构的定义:

您可以从那里开始工作,也可以在这里获取此脚本:


以下是jordan发布的功能的改进版本:

$vin = "1M8GDM9AXKP042788";

function validate_vin($vin) {

    $vin = strtolower($vin);
    if (!preg_match('/^[^\Wioq]{17}$/', $vin)) { 
        return false; 
    }

    $weights = array(8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2);

    $transliterations = array(
        "a" => 1, "b" => 2, "c" => 3, "d" => 4,
        "e" => 5, "f" => 6, "g" => 7, "h" => 8,
        "j" => 1, "k" => 2, "l" => 3, "m" => 4,
        "n" => 5, "p" => 7, "r" => 9, "s" => 2,
        "t" => 3, "u" => 4, "v" => 5, "w" => 6,
        "x" => 7, "y" => 8, "z" => 9
    );

    $sum = 0;

    for($i = 0 ; $i < strlen($vin) ; $i++ ) { // loop through characters of VIN
        // add transliterations * weight of their positions to get the sum
        if(!is_numeric($vin{$i})) {
            $sum += $transliterations[$vin{$i}] * $weights[$i];
        } else {
            $sum += $vin{$i} * $weights[$i];
        }
    }

    // find checkdigit by taking the mod of the sum

    $checkdigit = $sum % 11;

    if($checkdigit == 10) { // checkdigit of 10 is represented by "X"
        $checkdigit = "x";
    }

    return ($checkdigit == $vin{8});
}
$vin=“1M8GDM9AXKP042788”;
功能验证\u vin($vin){
$vin=strtolower($vin);
如果(!preg_match('/^[^\Wioq]{17}$/',$vin)){
返回false;
}
$weights=数组(8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2);
$translations=数组(
“a”=>1,“b”=>2,“c”=>3,“d”=>4,
“e”=>5,“f”=>6,“g”=>7,“h”=>8,
“j”=>1,“k”=>2,“l”=>3,“m”=>4,
“n”=>5,“p”=>7,“r”=>9,“s”=>2,
“t”=>3,“u”=>4,“v”=>5,“w”=>6,
“x”=>7,“y”=>8,“z”=>9
);
$sum=0;
对于($i=0;$i
以下是我使用维基百科文章中的示例快速编写的内容

不保证完美、无缺陷或超高效,但应为您提供坚实的起点:

注意:我包括了下面提供的编辑,使过程稍微简洁一些

function validate_vin($vin) {

    $vin = strtolower($vin);
    if (!preg_match('/^[^\Wioq]{17}$/', $vin)) { 
        return false; 
    }

    $weights = array(8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2);

    $transliterations = array(
        "a" => 1, "b" => 2, "c" => 3, "d" => 4,
        "e" => 5, "f" => 6, "g" => 7, "h" => 8,
        "j" => 1, "k" => 2, "l" => 3, "m" => 4,
        "n" => 5, "p" => 7, "r" => 9, "s" => 2,
        "t" => 3, "u" => 4, "v" => 5, "w" => 6,
        "x" => 7, "y" => 8, "z" => 9
    );

    $sum = 0;

    for($i = 0 ; $i < strlen($vin) ; $i++ ) { // loop through characters of VIN
        // add transliterations * weight of their positions to get the sum
        if(!is_numeric($vin{$i})) {
            $sum += $transliterations[$vin{$i}] * $weights[$i];
        } else {
            $sum += $vin{$i} * $weights[$i];
        }
    }

    // find checkdigit by taking the mod of the sum

    $checkdigit = $sum % 11;

    if($checkdigit == 10) { // checkdigit of 10 is represented by "X"
        $checkdigit = "x";
    }

    return ($checkdigit == $vin{8});
}
功能验证\u vin($vin){
$vin=strtolower($vin);
如果(!preg_match('/^[^\Wioq]{17}$/',$vin)){
返回false;
}
$weights=数组(8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2);
$translations=数组(
“a”=>1,“b”=>2,“c”=>3,“d”=>4,
“e”=>5,“f”=>6,“g”=>7,“h”=>8,
“j”=>1,“k”=>2,“l”=>3,“m”=>4,
“n”=>5,“p”=>7,“r”=>9,“s”=>2,
“t”=>3,“u”=>4,“v”=>5,“w”=>6,
“x”=>7,“y”=>8,“z”=>9
);
$sum=0;
对于($i=0;$i
注意:由于校验和的性质,验证VIN时有一个小的百分比错误:

…匹配不能证明VIN是正确的,因为任何两个不同的VIN有1/11的机会具有匹配的检查数字

另请注意:
11111111
将根据上述过程进行验证。您是否要检查这一点取决于您:

直接的数字(十七个连续的“1”)就足够了。这是因为一的值乘以89(权重之和)仍然是89。89%11是1,校验位。这是测试VIN检查算法的简单方法


参考资料:

这是jordan移植到Javascript的代码版本,希望对其他人有所帮助

function validate_vin(vin)
{
  function isnumeric(mixed_var) {
    return (typeof(mixed_var) === 'number' || typeof(mixed_var) === 'string') && mixed_var !== '' && !isNaN(mixed_var);
  }
  var pattern = /^[^\Wioq]{17}$/;
  var weights = Array(8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2);
  var transliterations = {
    "a" : 1, "b" : 2, "c" : 3, "d" : 4,
    "e" : 5, "f" : 6, "g" : 7, "h" : 8,
    "j" : 1, "k" : 2, "l" : 3, "m" : 4,
    "n" : 5, "p" : 7, "r" : 9, "s" : 2,
    "t" : 3, "u" : 4, "v" : 5, "w" : 6,
    "x" : 7, "y" : 8, "z" : 9
  };

  vin = vin.toLowerCase();
  if(!vin.match(pattern)) { return false; }

  var sum = 0;
  for(var i=0; i<vin.length; i++) {
    if(!isnumeric(vin.charAt(i))) {
      sum += transliterations[vin.charAt(i)] * weights[i];
    } else {
      sum += parseInt(vin.charAt(i)) * weights[i];
    }  
  }

  var checkdigit = sum % 11;
  if(checkdigit == 10) { // check digit of 10 represented by X
    checkdigit = 'x';
  }

  return (checkdigit == vin.charAt(8));
}
功能验证\u vin(vin)
{
函数为数值(混合变量){
return(typeof(mixed_-var)==“number”| | typeof(mixed_-var)==“string”)和&mixed_-var!==''&&&isNaN(mixed_-var);
}
变量模式=/^[^\Wioq]{17}$/;
变量权重=数组(8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2);
var音译={
“a”:1,“b”:2,“c”:3,“d”:4,
“e”:5,“f”:6,“g”:7,“h”:8,
“j”:1,“k”:2,“l”:3,“m”:4,
n:5,p:7,r:9,s:2,
“t”:3,“u”:4,“v”:5,“w”:6,
“x”:7,“y”:8,“z”:9
};
vin=vin.toLowerCase();
如果(!vin.match(pattern)){返回false;}
var总和=0;

对于(var i=0;i我最近不得不用PHP编写一个VIN验证类。我发布了我的类供大家使用,网址为:

类别VIN
{
公共静态$音译=数组(
“A'=>1,'B'=>2,'C'=>3,'D'=>4,'E'=>5,'F'=>6,'G'=>7,'H'=>8,
'J'=>1,'K'=>2,'L'=>3,'M'=>4,'N'=>5,'P'=>7,'R'=>9,
'S'=>2,'T'=>3,'U'=>4,'V'=>5,'W'=>6,'X'=>7,'Y'=>8,'Z'=>9,
);
公共静态$weights=数组(8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2);
/***
*校验和方法用于验证VIN是否有效
*它将返回一个包含两个键的数组:status和message
*“状态”将为布尔值TRUE或FALSE
*“消息”将是描述状态的字符串
*/
公共静态函数校验和($vin)
{
$vin=strtoupper($vin);
$length=strlen($vin);
$sum=0;
如果($length!=17)
{
返回数组('status'=>false,'message'=>'VIN的长度不正确');
}
对于($x=0;$xfalse,'message'=>'VIN包含无效字符');
}
$sum+=self::$translation[$char]*self::$weights[$x];
}
}
$余数=$sum%11;
$checkdigit=$rements==10?'X':$rements;
if(substr($vin,8,1)!=$checkdigit)
{
返回数组('status'=>false,'message'=>'VIN无效');
}
复述
class VIN
{
    public static $transliteration = array(
        'A'=>1, 'B'=>2, 'C'=>3, 'D'=>4, 'E'=>5, 'F'=>6, 'G'=>7, 'H'=>8, 
        'J'=>1, 'K'=>2, 'L'=>3, 'M'=>4, 'N'=>5, 'P'=>7, 'R'=>9,
        'S'=>2, 'T'=>3, 'U'=>4, 'V'=>5, 'W'=>6, 'X'=>7, 'Y'=>8, 'Z'=>9,
    );

    public static $weights = array(8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2);

    /***
     * The checksum method is used to validate whether or not a VIN is valid
     * It will return an array with two keys: status and message
     * The "status" will either be boolean TRUE or FALSE
     * The "message" will be a string describing the status
     */
    public static function checksum($vin)
    {
        $vin = strtoupper($vin);
        $length = strlen($vin);
        $sum = 0;

        if($length != 17)
        {
            return array('status'=>false, 'message'=>'VIN is not the right length');
        }

        for($x=0; $x<$length; $x++)
        {
            $char = substr($vin, $x, 1);

            if(is_numeric($char))
            {
                $sum += $char * self::$weights[$x];
            }
            else
            {
                if(!isset(self::$transliteration[$char]))
                {
                    return array('status'=>false, 'message'=>'VIN contains an invalid character.');
                }

                $sum += self::$transliteration[$char] * self::$weights[$x];
            }
        }

        $remainder = $sum % 11;
        $checkdigit = $remainder == 10 ? 'X' : $remainder;

        if(substr($vin, 8, 1) != $checkdigit)
        {
            return array('status'=>false, 'message'=>'The VIN is not valid.');
        }

        return array('status'=>true, 'message'=>'The VIN is valid.');
    }
}
  <?php
  /*
  =======================================================================================
  PURPOSE: VIN Validation (Check-digit validation is compulsory for all road vehicles sold in North America.)
  DETAILS: Validates 17 digit VINs by checking their formatting
  USAGE:  returns boolean or returns an array with a detailed message
  COMMENTS: This could be made more robust by checking the country codes etc..
  MORE INFO: https://en.wikipedia.org/wiki/Vehicle_identification_number
  =======================================================================================
  */

  class vinValidation {

public static $transliteration = array(
    'A'=>1, 'B'=>2, 'C'=>3, 'D'=>4, 'E'=>5, 'F'=>6, 'G'=>7, 'H'=>8,
    'J'=>1, 'K'=>2, 'L'=>3, 'M'=>4, 'N'=>5, 'P'=>7, 'R'=>9,
    'S'=>2, 'T'=>3, 'U'=>4, 'V'=>5, 'W'=>6, 'X'=>7, 'Y'=>8, 'Z'=>9,
);

public static $weights = array(8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2);

public function validateVIN($vin, $ret_array_status = false, $year = null) {

    //validates US/NA 1980>= VINs, if before 1980, no vin standards, this returns false
    if (!empty($year) && preg_match("/^[0-9]{4}/", $year)) {
        if ($year < 1980) return ($ret_array_status ? array('status' => false, 'message' => 'Unable to check VIN, pre-dates 1980.') : false);
    }

    $vin_length = 17; // US vin requirements >= 1980
    $vin = strtoupper(trim($vin));
    $sum = 0;

     //if (!preg_match('/^[^\Wioq]{17}$/', $vin))
    //return ($ret_array_status ? array('status'=>false, 'message'=>'VIN is not valid, not the right length.') : false);

    if (!preg_match('/^[A-HJ-NPR-Z0-9]{17}$/', $vin))
    return ($ret_array_status ? array('status'=>false, 'message'=>'VIN is not valid, VIN formatting is incorrect.') : false);

    if (preg_match('/(\w)\1{5,}/', $vin))
    return ($ret_array_status ? array('status'=>false, 'message'=>'VIN contains invalid repeating character sequence.') : false);

    for($x=0; $x < $vin_length; $x++) {
        $char = substr($vin, $x, 1);
        if(is_numeric($char)) {
            $sum += $char * self::$weights[$x];
        } else {
            if(!isset(self::$transliteration[$char]))
            return ($ret_array_status ? array('status'=>false, 'message'=>'VIN contains an invalid character.') : false);
            $sum += self::$transliteration[$char] * self::$weights[$x];
        }
    }
    $remainder = $sum % 11;
    $checkdigit = $remainder == 10 ? 'X' : $remainder;

    //echo " sum:".$sum." remain:".$remainder." check dig:".$checkdigit."\n";

    if(substr($vin, 8, 1) != $checkdigit)
    return ($ret_array_status ? array('status'=>false, 'message'=>'The VIN is not valid, failed checksum.') : false);

    // all is good return true or a value and status.
    return ($ret_array_status ? array('status'=>true, 'message'=>'The VIN is valid, passed checksum.') : true);
}


  }
  $vinClass = new vinValidation();

  // not long enough not val
  var_dump($vinClass->validateVIN('1I345678123456789', false, 2000));
  var_dump($vinClass->validateVIN('1I345678123456789'));

  echo "-----------------------------------------------------------\n";
  // not valid
  var_dump($vinClass->validateVIN('00000000012870842', true));
  var_dump($vinClass->validateVIN('00000000012870842', 1968)); //assumes faulty by year
  var_dump($vinClass->validateVIN('00000000012870842'));

  echo "-----------------------------------------------------------\n";
  // not valid
  var_dump($vinClass->validateVIN('00000000000354888', true));
  var_dump($vinClass->validateVIN('00000000000354888'));

  echo "-----------------------------------------------------------\n";
  // Fails Checksum test
  var_dump($vinClass->validateVIN('368TU79MXH4763452',false,2000));
  var_dump($vinClass->validateVIN('368TU79MXH4763452'));

  echo "-----------------------------------------------------------\n";
  // yachtzee, (returns true or array) !
  var_dump($vinClass->validateVIN('WP1AF2A56GLB91679',true));
  var_dump($vinClass->validateVIN('WP1AF2A56GLB91679'));
class VIN {
    static transliteration = {
        'A':1, 'B':2, 'C':3, 'D':4, 'E':5, 'F':6, 'G':7, 'H':8, 'J':1, 'K':2, 'L':3, 'M':4, 'N':5, 'P':7, 'R':9, 'S':2, 'T':3, 'U':4, 'V':5, 'W':6, 'X':7, 'Y':8, 'Z':9
    }

    static weights = [8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2];

    validateVIN(vin, retArrayStatus = false, year = null) {

        if (year != null && year.match(/^[0-9]{4}/)) {
            if (year < 1980)
                return retArrayStatus ? {'status': false, 'message': 'Unable to check VIN, pre-dates 1980.'} : false;
        }

        let vinLength = 17;
        vin = vin.trim();
        let sum = 0;

        if (!vin.match(/^[A-HJ-NPR-Z0-9]{17}$/))
            return retArrayStatus ? {'status': false, 'message': 'VIN is not valid, VIN formatting is incorrect [i, o, q].'} : false;

        //if (!vin.match(/(\w)\1{5,}/))
        //    return retArrayStatus ? {'status': false, 'message': 'VIN contains invalid repeating character sequence.'} : false;

        for (let x = 0; x < vinLength; x++) {
            let char = vin.substr(x, 1);
            if (!isNaN(char)) {
                sum += char * VIN.weights[x];
            }
            else {
                if (VIN.transliteration[char] == '')
                    return retArrayStatus ? {'status': false, 'message': 'VIN contains an invalid character.'} : false;
                sum += VIN.transliteration[char] * VIN.weights[x];
            }
        }
        let reminder = sum % 11;
        let checkdigit = reminder == 10 ? 'X' : reminder;

        if (vin.substr(8, 1) != checkdigit)
            return retArrayStatus ? {'status': false, 'message': 'The VIN is not valid, failed checksum.'} : false;

        return retArrayStatus ? {'status': true, 'message': 'The VIN is valid, passed checksum.'} : true;
    }
}