Kiểm tra số PHP

Nếu bạn tìm thấy bất kỳ lỗi nào hoặc có bất kỳ đề xuất nào để cập nhật thông tin, vui lòng cho chúng tôi biết hoặc tạo yêu cầu kéo trên GitHub

EAN13 là một định dạng mã vạch. Nó bao gồm 12 số mà bạn thường sẽ có một phạm vi được chỉ định cho mình. Chữ số thứ 13 được gọi là số kiểm tra và được tính bằng cách nhìn vào 12 chữ số khác. Mục đích của số kiểm tra là để đảm bảo rằng số đang được đọc chính xác vì nếu bất kỳ số nào không khớp, số kiểm tra sẽ không hợp lệ

Nếu bạn cần tạo các chữ số kiểm tra trong PHP, thì đây là chức năng tiện dụng của tôi

function ean13_check_digit[$digits]{
//first change digits to a string so that we can access individual numbers
$digits =[string]$digits;
// 1. Add the values of the digits in the even-numbered positions: 2, 4, 6, etc.
$even_sum = $digits{1} + $digits{3} + $digits{5} + $digits{7} + $digits{9} + $digits{11};
// 2. Multiply this result by 3.
$even_sum_three = $even_sum * 3;
// 3. Add the values of the digits in the odd-numbered positions: 1, 3, 5, etc.
$odd_sum = $digits{0} + $digits{2} + $digits{4} + $digits{6} + $digits{8} + $digits{10};
// 4. Sum the results of steps 2 and 3.
$total_sum = $even_sum_three + $odd_sum;
// 5. The check character is the smallest number which, when added to the result in step 4,  produces a multiple of 10.
$next_ten = [ceil[$total_sum/10]]*10;
$check_digit = $next_ten - $total_sum;
return $digits . $check_digit;
}

Các tài nguyên liên quan đến mã vạch khác

http. //phpclasses. phụ kiện. com/duyệt/gói/3643. html

http. // thinkabdul. com/2007/01/04/barcoder-freeware-ean-13-barcode-reader-for-java-j2me-mobiles/

/**
 * Luhn algorithm [a.k.a. modulus 10] is a simple formula used to validate variety of identification numbers.
 * It is not intended to be a cyrptographically secure hash function, it was designed to protect against accidental errors.
 * See //en.wikipedia.org/wiki/Luhn_algorithm
 *
 * @author Rolands Kusiņš
 * @version 0.2
 * @license GPL
 */

class Luhn {
    private $sumTable = array[array[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], array[0, 2, 4, 6, 8, 1, 3, 5, 7, 9]];

    /**
     * Calculate check digit according to Luhn's algorithm
     * New method [suggested by H.Johnson], see //www.phpclasses.org/discuss/package/8471/thread/1/
     *
     * @param string $number
     * @return integer
     */
    public function calculate[$number]
    {
        $length = strlen[$number];
        $sum = 0;
        $flip = 1;
        // Sum digits [last one is check digit, which is not in parameter]
        for[$i = $length-1; $i >= 0; --$i] $sum += $this->sumTable[$flip++ & 0x1][$number[$i]];
        // Multiply by 9
        $sum *= 9;
        // Last digit of sum is check digit
        return [int]substr[$sum, -1, 1];
    }

    /**
     * Calculate check digit according to Luhn's algorithm
     * This is an old method, tests show that this is little bit slower than new one
     *
     * @param string $number
     * @return integer
     */
    public function calculateOld[$number] {
        $length = strlen[$number];
        $sum = 0;
        $p = $length % 2;
        // Sum digits, where every second digit from right is doubled [last one is check digit, which is not in parameter]
        for[$i = $length-1; $i >= 0; --$i] {
            $digit = $number[$i];
            // Every second digit is doubled
            if [$i % 2 != $p] {
                $digit *= 2;
                // If doubled value is 10 or more [for example 13], then add to sum each digit [i.e. 1 and 3]
                if[$digit > 9]{
                    $sum += $digit[0];
                    $sum += $digit[1];
                } else{
                    $sum += $digit;
                }
            } else{
                $sum += $digit;
            }
        }
        // Multiply by 9
        $sum *= 9;
        // Last one is check digit
        return [int]substr[$sum, -1, 1];
    }

    /**
     * Validate number against check digit
     *
     * @param string $number
     * @param integer $digit
     * @return boolean
     */
    public function validate[$number,$digit]{
        $calculated = $this->calculate[$number];
        if[$digit == $calculated] return true;
        else return false;
    }
}
?>

Đây là một hàm php [hàmcalcul_upc_check_digit[$upc_code]] hôm nay tôi đã viết để tính số kiểm tra của Mã UPC-A [12 chữ số]. Bạn có thể sử dụng nó để xác thực Mã UPC hoặc để tính số kiểm tra khi nhà sản xuất chỉ cung cấp 11 chữ số của mã UPC

Tập lệnh sẽ chấp nhận Mã UPC có độ dài 11 hoặc 12 và trả về số kiểm tra đã tính toán. Nếu độ dài của Mã UPC khác thì 11 hoặc 12 sẽ trả về -1


// ----------------------------------------------------
// calculate check digit for upc
// parameter UPC can be either 11 digits [to calculate]
// or full 12 digits to use to validate
// returns -1 on failure
// or check digit 0 - 9 on success
// to validate upc send 12 digit upc code in
// and compare returned value with the 12th
// digit of UPC code you are validating
// --------------------------------------------------
function calculate_upc_check_digit[$upc_code] {
$checkDigit = -1; // -1 == failure
$upc = substr[$upc_code,0,11];
// send in a 11 or 12 digit upc code only
if [strlen[$upc] == 11 && strlen[$upc_code]

Chủ Đề