Recently in the preparation of a book management program, need to check the book ISBN correct, check some information, understand the book ISBN code composition, detailed information can refer to: ISBN-wikipedia, below or briefly 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
/** * Name: PHP check the ISBN code function * Author: Lucy Dou * Blog: http://www.ludou.org/ * 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:
1 |
<?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