PHP Check ISBN code function code _php tips

Source: Internet
Author: User
More information can be referred to: ISBN-Wikipedia, the following is a brief description of what is the ISBN code:
ISBN (International Standard book NUMBER,ISBN, Is-ben), is the international general books or independent publications (except periodical published periodicals) code. Publishers can clearly identify all non-journal books through International ISBN. One ISBN only one or a corresponding publication corresponds to it. If the new version does not have much change on the basis of the original version, the new international standard Book number will not be available at the time of publication. When the paperback is printed in hardback, the corresponding ISBN number should also be withdrawn.
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:
Copy Code code as follows:

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:
Copy Code code as follows:

<?php echo is_isbn (' 9787507421781 ')? ' Checksum passed ': ' checksum failed ';?>

In addition, I have written an online check ISBN tool, which can be used to check the legality of the ISBN Code online, you can click the following link to use: ISBN Code Online Check tool

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.