A detailed explanation of the function of PHP _php for the ISBN Code Tutorial

Source: Internet
Author: User
This article to tell about the ISBN check method, there is a need for students to refer to. Considering a rigorous book management process to take into account a variety of issues, because the 10-bit ISBN code Books still have a huge amount of storage, so to verify the correctness of the book ISBN code, you must consider both 10-bit and 13-bit situation.

Usually we can see the ISBN code has 10-bit and 13-bit, of which 10-bit ISBN has been discontinued since January 2007, the current new book ISBN code is 13 bits. Considering a rigorous book management process to take into account a variety of issues, because the 10-bit ISBN code Books still have a huge amount of storage, so to verify the correctness of the book ISBN code, you must consider both 10-bit and 13-bit situation. From Wikipedia can be learned that the last one is the ISBN code, in fact, to verify the correct ISBN code, is to calculate the ISBN check code, see if it matches the last one. The checksum here is only verifying that the ISBN is legitimate on the composition, and does not verify that the ISBN is a published book. The following is an ISBN code verification algorithm provided by Wikipedia:

The calculation method of the check code (10 yards)
Assuming some ISBN number the first 9 bits are: 7-309-04547
Calculate weighted and s:s = 7X10+3X9+0X8+9X7+0X6+4X5+5X4+4X3+7X2 = 226
Calculates the remainder of the s÷11 m:m = 226 MoD 11 = 6
Calculate the difference between 11–m n = 11−6 = 5
If n = 10, the check code is the letter "X"
If n = 11, the check code is the number "0"
If n is a different number, the checksum 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

The calculation method of the check code (13 yards)
Assuming some ISBN number the first 12 bits are: 978-986-181-728
Calculated weights 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 the difference between 10–m n = 10−4 = 6
If n = 10, the check code is the number "0"
If n is a different number, the checksum 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 introduced to this, below 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: Lulu
* Last modified: September 26, 2010
*/

function Isbn_sum ($ISBN, $len)
{
/*
* This function is used to calculate 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 check digit
* 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 the 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)/* ISBN mantissa does not match the calculated check code */
return 0;
Else
return 1;
}

Once the function is written, it can be called directly, and here is an example of the invocation:

The code is as follows Copy Code

In addition, I have written an online verification of the ISBN tool, using the tool to check the validity of the ISBN code online


http://www.bkjia.com/PHPjc/444685.html www.bkjia.com true http://www.bkjia.com/PHPjc/444685.html techarticle This article to tell about the ISBN check method, there is a need for students to refer to. Considering a rigorous library management process to take into account a variety of issues, because the 10-bit ISBN ...

  • 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.