PHP the function of the ISBN code is detailed

Source: Internet
Author: User

Normally we can see the ISBN code has 10-bit and 13-bit two, of which 10 of the ISBN since January 2007 has ceased to use, the current new published book ISBN Code are 13. Considering a rigorous book management procedures to take into account a wide range of issues, because the 10-bit ISBN book still has a huge amount of storage, so to check the correctness of the book ISBN code, we must consider the case of 10-bit and 13-bit. From Wikipedia can understand the ISBN code last is the check code, in fact, to check the ISBN code is correct, is through the calculation of the ISBN check code, to see if the last one matches. Here the verification is only to check the composition of the ISBN is legal, and will not check whether the published book ISBN. The following is the ISBN code check algorithm provided by Wikipedia:

Calculation method of check code (10 yards)
Assume that the first 9 digits of a ISBN number are: 7-309-04547
Compute weighted and s:s = 7X10+3X9+0X8+9X7+0X6+4X5+5X4+4X3+7X2 = 226
Calculates the remainder m:m = s÷11 MoD 11 = 6
Calculate 11–m Difference n:n = 11−6 = 5
If n = 10, the checksum code is the letter "X"
If n = 11, the checksum code is the number "0"
If n is another number, the checksum code is the number n
Therefore, the check code of this book is 5; If the user provides an ISBN code of 7-309-04547-6, then the checksum fails

Calculation method of check code (13 yards)
Assume that the first 12 digits of a ISBN number are: 978-986-181-728
Calculate weighted and s:s = (9x1) + (7X3) + (8x1) + (9X3) + (8x1) + (6x3) + (1x1) + (8x3) + (1x1) + (7X3) + (2x1) + (8x3) = 164
Calculates the remainder of the s÷10 m:m = 164 mod 10 = 4
Calculate 10–m Difference n:n = 10−4 = 6
If n = 10, the checksum code is the number "0"
If n is another number, the checksum code is the number n
So, the check code for this book is 6. Complete ISBN number for ISBN 978-986-181-728-6

Well, background knowledge to this, the following I write the ISBN code check function (PHP version), if necessary, can be used directly

The code is as follows Copy Code

/**
* Name: PHP check the ISBN code function
* Author: Lucy Dou
* Last modified: September 26, 2010
*/

function Isbn_sum ($ISBN, $len)
{
/*
* This function is used to compute the ISBN weighting and
* Parameter Description:
* $ISBN: ISBN code
* $len: ISBN code length
*/
$sum = 0;

if ($len = = 10)
{
for ($i = 0; $i < $len-1; $i + +)
{
$sum = $sum + (int) $ISBN [$i] * ($len-$i);
}
}
ElseIf ($len = = 13)
{
for ($i = 0; $i < $len-1; $i + +)
{
if ($i% 2 = 0)
$sum = $sum + (int) $ISBN [$i];
Else
$sum = $sum + (int) $ISBN [$i] * 3;
}
}
return $sum;
}

function Isbn_compute ($ISBN, $len)
{
/*
* This function is used to calculate the ISBN bottom check code
* Parameter Description:
* $ISBN: ISBN code
* $len: ISBN code length
*/

if ($len = = 10)
{
$digit = 11-isbn_sum ($ISBN, $len)% 11;

if ($digit = = 10)
$RC = ' X ';
else if ($digit = = 11)
$RC = ' 0 ';
Else
$RC = (string) $digit;
}
else if ($len = = 13)
{
$digit = 10-isbn_sum ($ISBN, $len)% 10;

if ($digit = = 10)
$RC = ' 0 ';
Else
$RC = (string) $digit;
}

return $RC;
}

function Is_isbn ($ISBN)
{
/*
* This function is used to determine whether an ISBN number
* Parameter Description:
* $ISBN: ISBN code
*/
$len = strlen ($ISBN);

if ($len!=10 && $len!=13)
return 0;

$RC = Isbn_compute ($ISBN, $len);

if ($ISBN [$len-1]!= $RC)/* The ISBN mantissa does not match the computed check code
return 0;
Else
return 1;
}

Once the function is written, you can call it directly, and here is the call example:

The code is as follows Copy Code
<?php echo is_isbn (' 9787507421781 ')? ' Checksum passed ': ' checksum failed ';?>

In addition, I wrote an online checkout tool, using the tool to verify the validity of the ISBN code online

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.