一次对多个 IMEI 进行 IMEI 校验位 PHP

IMEI check digit on more than one IMEI at a time with PHP

我正在尝试制作一个网站,您可以在一页(下)中输入非校验位 IMEI 的列表

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<link href='http://fonts.googleapis.com/css?family=Reenie+Beanie' rel='stylesheet' type='text/css'>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>IMEI check digit</title> 
    <link rel="shortcut icon" href="https://c2.staticflickr.com/8/7203/6890418511_fc92574c8a.jpg"/>
    <link rel="alternate" type="application/rss+xml" href="{RSS}" />
        <style type="text/css">
        /*Main CSS for the body*/
        body {
            background: url("http://static.tumblr.com/225d38dfe37f2d9a7edb3e1fbfcee711/ufgtjry/8J2nks995/tumblr_static_dnfrmr3xbjwckcwckckwocgo0.jpg")
            no-repeat center center fixed; /*CHANGE BACKGROUND IMAGE HERE*/
            -webkit-background-size: cover;
            -moz-background-size: cover;
            -o-background-size: cover;
            background-size: cover;
            font-style:none;
            font-family:calibri;
            color:{color:text};
            font-size:12px;}

        body, a, a:hover {
            cursor: url(http://img69.imageshack.us/img69/7673/cursorw.png), progress;}

        html, body { height: 100%; margin: 0; }

        /*CSS used for box itself*/

        #transbox {
            opacity: 0.7;
            filter: alpha(opacity=60);
            position: relative;
            width: 600px;
            background-color: #fff;
            border: 1px solid #000;
            padding:4px;}

        #transbox a:hover {
            opacity: 1.0;
            filter: alpha(opacity=100);}

        #container {
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;}

        /*CSS used for title*/

        #title {
            font-family: calibri;
            font-size:18px;
            position: relative;
            text-transform:uppercase;
            top: 8px;
            left: 8px;
            letter-spacing: 3px;
            color:#000;
            margin-left:-5px;
            z-index:1;
            text-align: center;}

        #title:hover {
            font-style:italic;}

        /*CSS used for nav links*/

        #navigation a {
            font-family:'calibri';
            display:block;
            color:#000;
            font-size:12px;
            margin-top: 22px;
            padding:6px;
            text-align:center;
            letter-spacing:2px;
            text-transform:uppercase;
            text-decoration:underline;
            margin-bottom:5px;
            -webkit-transition: all 0.4s ease-in-out;
            -moz-transition: all 0.4s ease-in-out;
            -o-transition: all 0.4s ease-in-out;
            -ms-transition: all 0.4s ease-in-out;
            transition: all 0.4s ease-in-out;}


        #navigation a:hover {
            background:#999;
            color:#fff;
            opacity: 1.0;
            text-decoration: none;
            filter: alpha(opacity=100);}

        #right{ 
            text-align:right;}

        #center{ 
            text-align:center;}

        p{
            text-align:center;}
        </style>
</head>
<body>
    <div id="container">
    <div id="transbox">
    <div id="title">Check digit generator</div>
    <form action="output.php" method="POST">
        <p><strong>The 14 digit imei</strong></p>
        <textarea required id="IMEI" rows="16" class="font-fixed-width" style="width: 90%;margin : 0 auto; display: block" name="IMEI"></textarea>
        </br>
        <p><div id="center"><input type="submit"></div></p>
    </form>
    </div> 
    </div>
</body>
</html>

然后提交到这个页面

<?php
$imei = $_POST['IMEI']; //chk has to be 8

//select numbers from imei and add to array
for ($i = 0; $i <= 14; $i++) {
    $D[]=$imei;
}
$sumeven = 0;
// adding even numbers together
for ($i = 1; $i <= 14; $i=$i+2) {
    $sum = $D[$i]*2;

        //seperate numbers that become double digits
        if ($D[$i] > 4) {
            //convert int to string
            $sumS = (string)$sum;
            //clear array
            unset($splitt);
            //seperate the numbers and add them together
            for ($j = 0; $j <= 1; $j++) {
                $splitt[]=$sumS[$j];
                $sumeven = $sumeven + $splitt[$j];
            }
        //else just add the single number to the last   
        } else {
            $sumeven = $sumeven + $sum;
        }
}
// adding odd numbers together
$sumodd = 0;
$sum = 0;
for ($i = 0; $i <= 13; $i=$i+2) {
    $sum = $D[$i];
    $sumodd = $sumodd + $sum;
}
// add odd and even numbers
$total = $sumodd + $sumeven;
//convert int to string
$totalS = (string)$total;
//Split total into two variables
for ($i = 0; $i <= 1; $i++) {
    $split[]=$totalS[$i];
}
if ($split[1] == 0) {
    $zero = 0;
    $IMEIZERO = $imei.$zero;
    //print $IMEIZERO;
} else {
    $split[0] = $split[0] + 1;
    $totalNew = $split[0]."0";
    $chk = $totalNew - $total;

    $IMEICHK = $imei.$chk;
    //print $IMEICHK;
} 


?>
<html>
<head>
</head>
<body>
<textarea id="txtList" rows="16" class="font-fixed-width" style="width: 100%;" name="txtList">
<?php print $IMEICHK OR $IMEIZERO?>
</textarea>
</body>
</html>

但是我很难考虑如何让它打印或回显不止一次,并且 print/echo 将唯一值放入文本区域,例如,如果我想获得以下 14 位的校验位数字 IMEI 15832859381748 18247395837243 52835621351219 12583582923852

我想把它们放在第一页,按提交,然后放在第二页,最后是校验位。所以像这样

158328593817484 182473958372434 528356213512196 125835829238525

有什么想法吗?

非常感谢

让用户每行输入一个数字,然后当您收到它时,使用 PHP 的 explode() 函数。您最终会得到一个包含所有 IMEI 的数组。

我忍不住要修复校验和计算的冲动,有内联注释解释了一些函数的作用。

$imeis = explode("\n", $_POST['IMEI']);
$output = "";
foreach ($imeis as $imei) {
    $sum = 0;
    //str_split converts strings to arrays
    //array_reverse reverses arrays; that is how the checksum should be done
    $imeiRev = array_reverse(str_split(trim($imei)));
    //loop through each digit in the now-reversed IMEI
    foreach ($imeiRev as $i=>$digit) {
        if ($i % 2 === 0 && $digit > 4) {
            //it's even and greater than 4
            $digit = array_sum(str_split($digit * 2));
        } elseif ($i % 2 === 0) {
            //it's even
            $digit = $digit * 2;
        }
        //add to our running total
        $sum += $digit;
    }
    $sum = str_split($sum * 9);
    //array_pop just "pops" the last value off
    $checkdigit = array_pop($sum);
    //add it to the IMEI
    $output .= "$imei$checkdigit\n";
}

?>
<html>
    <head>
        <title>Checksum Results</title>
    </head>
    <body>
        <textarea id="txtList" rows="16" class="font-fixed-width" style="width: 100%;" name="txtList">
        <?php echo $output?>
        </textarea>
    </body>
</html>