IMEI使用PHP一次检查多个IMEI上的数字


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>

然而,我很难思考如何让它打印或回显多次,并将唯一值打印/回显到文本区域中,例如,如果我想获得以下14位IMEI的校验位15832859381748182473958372435283562135121912583582923852

我想把它们放在第一页,按提交,然后放在第二页,最后加上校验数字。就像这个

158328593817484182473958372434528356213512196125835829238525

有什么想法吗?

非常感谢

让用户每行输入一个数字,然后当您收到它时,使用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>