I’m new to PHP you will see why in the code below. I’m trying to convert a string to only numbers, then multiply every number according to its position and then sum all up to one variable and divide it by 11 in the end. But end up getting a too big of a value in the end.
It all starts with getting a string of 17 letters and numbers, which will be converted only to numbers:
<?php
session_start();
if(!isset($_POST['submit']))
{
//$_SESSION['count']=0;
}
?>
<form action="" method="POST">
<i> VIN-nummer: </i><input type="text" name="fvin">
<input type="submit" value="Beräkna" name="submit">
<br></br>
</form>
<?php
$fvin = $_POST['fvin'];
$l2n = 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'
);
$keys = array_keys($l2n);
$values = array_values($l2n);
$evin = str_replace($keys, $values, $fvin);
I have echoed the results and so far so good! Then for the "broken" part. For every position a number is in, it will be multiplied with a specific number e.g:
- Position 0 needs to be multiplied with 8
- Position 1 needs to be multiplied with 7
- Position 2 needs to be multiplied with 6…
$fvin0 = substr($evin, 0, 0). $fvin0 *=8;
$fvin1 = substr($evin, 1, 1). $fvin1 *=7;
$fvin2 = substr($evin, 2, 2). $fvin2 *=6;
$fvin3 = substr($evin, 3, 3). $fvin3 *=5;
$fvin4 = substr($evin, 4, 4). $fvin4 *=4;
$fvin5 = substr($evin, 5, 5). $fvin5 *=3;
$fvin6 = substr($evin, 6, 6). $fvin6 *=2;
$fvin7 = substr($evin, 7, 7). $fvin7 *=10;
$fvin8 = substr($evin, 8, 8);
$fvin9 = substr($evin, 9, 9). $fvin9 *=9;
$fvin10 = substr($evin, 10, 10). $fvin10 *=8;
$fvin11 = substr($evin, 11, 11). $fvin11 *=7;
$fvin12 = substr($evin, 12, 12). $fvin12 *=6;
$fvin13 = substr($evin, 13, 13). $fvin13 *=5;
$fvin14 = substr($evin, 14, 14). $fvin14 *=4;
$fvin15 = substr($evin, 15, 15). $fvin15 *=3;
$fvin16 = substr($evin, 16, 16). $fvin16 *=2;
Every number will be multiplied with a number except position 8.
Then sum everything together up and divide it by 11.
$fvin += ($fvin0 + $fvin1 + $fvin2 + $fvin3 + $fvin4 + $fvin5 + $fvin6 + $fvin7 + $fvin8 + $fvin9 + $fvin10 + $fvin11 + $fvin12 + $fvin13 + $fvin14 + $fvin15 + $fvin16)/11;
if(isset($_POST['submit']))
{
echo $fvin;
echo "<br>";
}
I’ve done the math on paper and I get too big of a number. Also here’s an example of what string I start with: VRZUA5FX29J276031, and should end up with 42,363636… but end up with 94405436.6363…
I know my code is a bit messy but any help would make my day!
>Solution :
You shouldn’t be concatenating $fvin<num> *= multiplier to each value. Since you haven’t assigned anything to any of those $fvin variables yet, you’re multiplying 0, and then appending that 0 to the substring of $evin. You should be multiplying the substring by those multipliers.
The second argument to substr() is the length of the substring, not the index of the end of the substring. These should all be 1.
$fvin0 = substr($evin, 0, 1) * 8;
$fvin1 = substr($evin, 1, 1) * 7;
$fvin2 = substr($evin, 2, 1) * 6;
$fvin3 = substr($evin, 3, 1) * 5;
$fvin4 = substr($evin, 4, 1) * 4;
$fvin5 = substr($evin, 5, 1) * 3;
$fvin6 = substr($evin, 6, 1) * 2;
$fvin7 = substr($evin, 7, 1) * 10;
$fvin8 = substr($evin, 8, 1);
$fvin9 = substr($evin, 9, 1) * 9;
$fvin10 = substr($evin, 10, 1) * 8;
$fvin11 = substr($evin, 11, 1) * 7;
$fvin12 = substr($evin, 12, 1) * 6;
$fvin13 = substr($evin, 13, 1) * 5;
$fvin14 = substr($evin, 14, 1) * 4;
$fvin15 = substr($evin, 15, 1) * 3;
$fvin16 = substr($evin, 16, 1) * 2;