1/30/2013

International Standard Serial Number (ISSN) checker algorithm based on PHP

An International Standard Serial Number (ISSN) is a unique eight-digit number used to identify a print or electronic periodical publication.

Algorithm
1. Calculate the sum of the first seven digits of the ISSN multiplied by its position in the number, counting from the right — that is, 8, 7, 6, 5, 4, 3, and 2, respectively.

2.  The modulus 11 of this sum is then calculated: divide the sum by 11 and determine the remainder.

3. If there is no remainder the check digit is 0, otherwise the remainder value is subtracted from 11 to give the check digit: 11 -remainder

4. An upper case X in the check digit position indicates a check digit of 10.

5. To confirm the check digit, calculate the sum of all eight digits of the ISSN multiplied by its position in the number, counting from the right (if the check digit is X, then add 10 to the sum). The modulus 11 of the sum must be 0.

PHP Source Code

$issn = '03785955';
if($issn == checker($issn)){
echo 'good';
}else{
echo 'wrong';
}

function checker($issn=0,$chechsum = 0) {              
        $issn = substr($issn, 0, 7);
     
        foreach (str_split($issn) as $key => $value) {
            $chechsum += (8 - $key) * $value;
        }
     
        $chechsum = $chechsum % 11;
        if ($chechsum) {
            $chechsum = 11 - $chechsum;
            if ($chechsum == 10) {
                $chechsum = 'X';
            }
        }      

        return $issn.$chechsum;
    }

or One improved performance algorithms



function checker($issn = 0, $chechsum = 0) {
        $issn = substr(preg_replace('/[^0-9]+/', '', $issn), 0, 7);

        if (strlen($issn) == 7) {
            foreach (array_reverse(str_split((int) $issn)) as $key => $value) {
                if ($value) {
                    $key = $key + 2;
                    $chechsum = $key * $value + $chechsum;
                }
            }

            $chechsum %=11;
            if ($chechsum) {
                $chechsum = 11 - $chechsum;
                if ($chechsum == 10) {
                    $chechsum = 'X';
                }
            }

            return $issn . $chechsum;
        } else {
            return '';
        }
    }

1 comment:

  1. There is a very excellent ISSN verifier here: http://journal-index.org/ISSN-validator/

    ReplyDelete