PHP ISBN code verification function

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags check clear code function information internationally network network programming

Recently in the preparation of a library management program, you need to verify the correctness of the ISBN code of the book, check some information to understand the composition of the ISBN code of the book, the details you can refer to: ISBN - Wikipedia, the following is a brief description of what ISBN code:

The International Standard Book Number (ISBN; is-ben), is an internationally accepted book or stand-alone publication (except for regularly published journals). Publishers can clearly identify all non-journal books through the ISBN. Only one or a corresponding publication of an ISBN corresponds to it. If the new version does not change much in content based on the old version, it will not get a new ISBN number at the time of publication. When the paperback was changed to hardcover, the original corresponding ISBN should also be withdrawn.

Usually we can see the ISBN code 10 and 13 two, of which 10 ISBN as of January 2007 has been discontinued, the current publication of ISBN code book is 13. Taking into account a rigorous library management procedures to take into account a wide range of issues, because 10 ISBN code books still have a tremendous amount of survival, so to verify the correctness of ISBN code of books, we must also consider the 10 and 13 Bit situation. From Wikipedia can understand ISBN code is the last one check code, in fact, want to verify the ISBN code is correct, is calculated by ISBN check code to see whether the last one coincides. The verification here is only to verify whether the ISBN is legal in composition, and not to check if it is an ISBN of the published book. The following is an ISBN code verification algorithm provided by Wikipedia:

Check code calculation method (10 yards)

Suppose that the first nine digits of an ISBN are: 7-309-04547

Weighted sum S is calculated: S = 7 × 10 + 3 × 9 + 0 × 8 + 9 × 7 + 0 × 6 + 4 × 5 + 5 × 4 + 4 × 3 + 7 × 2 = 226

Calculate the remainder of S ÷ 11 M: M = 226 mod 11 = 6

Calculate the difference 11 - M N: 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 other numbers, the check code is the number N

Therefore, the book's verification code is 5; if the user provides the ISBN code is 7-309-04547-6, then the verification fails

Check code calculation method (13 yards)

Suppose that the first 12 digits of an ISBN are: 978-986-181-728

The weighted sum S is calculated: S = 9 × 1 + 7 × 3 + 8 × 1 + 9 × 3 + 8 × 1 + 6 × 3 + 1 × 1 + 8 × 3 + 1 × 1 + 7 × 3 + 2 × 1 + 8 × 3 = 164

Calculate the remainder of S ÷ 10 M: M = 164 mod 10 = 4

Calculate the difference 10 - M N: N = 10? 4 = 6

If N = 10, the check digit is the number "0"

If N is other numbers, the check code is the number N

So, the book's check code is 6. The complete ISBN ISBN 978-986-181-728-6

Well, background information introduced to this, I wrote below ISBN code verification function (php version), if necessary, can be used directly:

function isbn_sum ($ isbn, $ len) {/ * * This function is used to calculate ISBN weighted sum * Parameter Description: * $ isbn: isbn code * $ len: isbn code length * / $ sum = 0; = 10) {for ($ i = 0; $ i <$ len-1; $ i ++) {$ sum = $ sum + (int) $ isbn [$ i] * ($ len - $ i); ($ len == 13) {for ($ i = 0; $ i <$ len-1; $ i ++) {if ($ i% 2 == 0) $ sum = $ sum + (int) $ isbn [$ ($ isbn, $ len) {/ * * This function is used to calculate the end of ISBN Check code * Parameter Description: * $ isbn: isbn code * $ len: isbn code length * / if ($ len == 10) {$ digit = 11 - isbn_sum ($ isbn, $ len)% 11; else if ($ digit == 10) $ rc = 'X'; else if ($ digit == 11) $ rc = '0'; else $ rc = (string) $ digit; len $ 13 = {$ digit = 10 - isbn_sum ($ isbn, $ len)% 10; if ($ digit == 10) $ rc = '0'; else $ rc = (string) $ digit;} return $ function is_isbn ($ isbn) {/ * * This function is used to determine whether the ISBN number * Parameter Description: * $ isbn: isbn code * / $ len = strlen ($ isbn); if ($ isbn [$ len-1]! = $ rc) / * The ISBN mantissa does not match the computed checksum. * && $ len! = 13) return 0; $ rc = isbn_compute ($ isbn, $ len) / return 0; else return 1;}

After the function is written, you can call directly, the following is an example of the call:

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

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.