#native_company# #native_desc#
#native_cta#

Vechicle VIN number check digit calculator

By John Frisbie
on November 23, 2002

Version: 1.0

Type: Full Script

Category: Algorithms

License: GNU General Public License

Description: Put in a 17 digit VIN number and it tells you if the check digit computes or not.
Future plans include extracting the manufacturer and other known information from the VIN. If anyone writes a class from this code please share here. phpbuilder rocks!

<?php
	if (!$vin){
		echo "<p>Enter VIN number:</p>";
		echo "<form method=post action="" . $_SERVER['PHP_SELF'] . "">";
		echo "<p><INPUT TYPE="TEXT" MAXLENGTH="17" SIZE="31" NAME="vin"></p>";
		echo "<p><INPUT TYPE="SUBMIT" VALUE="Check It"></p>";
		echo "</form>";
		exit;}
// check vin lenth
	if (strlen($vin) != "17"){
		echo "ERROR - VIN lenth must be 17 characters long.<br>You only entered " . strlen($vin) . " characters.";
		exit;}
// make VIN all caps 
$vin = strtoupper($vin);
// model years 1980 - 2000
$MODEL_YEARS = array("A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R", "S", "T", "V", "W", "X", "Y");
// weights for multiplyer
$WEIGHTS = array("8", "7", "6", "5", "4", "3", "2", "10", "9", "8", "7", "6", "5", "4", "3", "2");
// characters
$CHARS = array("A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
// character values
$CHAR_VALUES = array("1", "2", "3", "4", "5", "6", "7", "8", "1", "2", "3", "4", "5", "7", "9", "2", "3", "4", "5", "6", "7", "8", "9", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
// makea new array
$VIN_CHARS = array();
$count = 0;
	foreach ($CHARS as $char){
		$VIN_CHARS[$char] = $CHAR_VALUES[$count];
		$count++;}		
// check digit 0 - 9 and 10 = X 
$CHECK_DIGITS = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "X");
// push each character of the vin into an array removing the 9th character (check digit)
echo "Your VIN's check digit is " . substr($vin, 8, 1);
echo "<br>";
$count = 0;
$vin_array = array();
	for ($i=0;$i<17;$i++){
		if ($i!=8){$vin_array[$count] = substr($vin, $i, 1);
			$count++;}}
// preform the math
$total = 0;
	for ($i=0;$i<16;$i++){
		$this_digit = $vin_array[$i];
		$this_total = $WEIGHTS[$i] * $VIN_CHARS[$this_digit];
		$total = $total + $this_total;
		}
$remainder = fmod($total, 11);
	if (substr($vin, 8, 1)!= $CHECK_DIGITS[$remainder]){
		echo "ERROR - Check digit does not compute.<br>Recheck your VIN number.<br>";
		echo "Computed check digit: $CHECK_DIGITS[$remainder]<br>";
		echo "Your check digit: " . substr($vin, 8, 1) . "<br>";
	 	}
	else {echo "Computed check digit: $CHECK_DIGITS[$remainder]<br>";
		echo "VIN number seems to be valid.<br>";}
?>