PHP check ISBN code function code

  • 2020-03-31 21:26:07
  • OfStack

Details you can refer to: international standard book number (ISBN) � (link: http://zh.wikipedia.org/zh-cn/%E5%9B%BD%E9%99%85%E6%A0%87%E5%87%86%E4%B9%A6%E5%8F%B7), or briefly what is the ISBN code below:
International Standard Book Number (ISBN); Quasi - pronounced is-ben, is an international book or independent publication code (except for periodical publications). Publishers can clearly identify all non-periodical books by their isbn Numbers. An isbn has only one or one corresponding publication. The new edition will not receive the new international standard book number if there is no major change in content from the previous edition. When the paperback is changed to the hardcover, the corresponding isbn number should be withdrawn.
There are two types of ISBN codes in common use: 10 bits and 13 bits. The 10 bit ISBN has been out of use since January 2007. Considering that a rigorous library management program to consider many aspects of the problem, because the 10-bit ISBN code book or a huge amount of existence, so to verify the correctness of the book ISBN code, must consider both 10 and 13. According to wikipedia, the last digit of ISBN code is a check code. In fact, to verify the correctness of ISBN code, it is to calculate the check code of ISBN to see whether it matches the last digit. The check here is only to verify that the ISBN is constitutionally legal, not that it is an ISBN of a published book. Here is the ISBN code check algorithm provided by wikipedia:
Calculation method of check code (10 code)
Suppose that the first nine digits of an isbn number are 7-309-04547
Calculate the weighted and S: S = 7 * 10 + 3 * 9 + 0 x 8 + 9 * 7 + 0 * 6 + 4 * 4 + 5 + 5 4 * 3 + 7 * 2 = 226
Calculate the remainder of S present 11 M: M = 226 mod 11 = 6
Calculate the difference N: N = 11? 6 = 5
If N is equal to 10, the checksum is the letter "X".
If N is equal to 11, the check number is zero.
If N is any other number, the check number is N
So the check code for this book is 5; If the ISBN code provided by the user is 7-309-04547-6, the check fails
Calculation method of check code (13 code)
Suppose that the first 12 digits of an isbn number are 978-986-181-728
Weighted and S: S = (9 * 1) + (7 * 3) + 8 x (1) + (9 x 3) + 8 x (1) + (6 * 3) + (1 * 1) + (8 x 3) + (1 * 1) + (7 * 3) + (2 * 1) + (8 x 3) = 164
Calculate the remainder M: M = 164 mod 10 = 4 of S present 10
Calculate the difference N: N = 10? 4 = 6
If N is equal to 10, the check number is zero.
If N is any other number, the check number is N
So the check code for this book is 6. The complete ISBN number is ISBN 978-986-181-728-6
Ok, the background is introduced here, the following I wrote ISBN code check function (PHP version), if necessary, you can directly use:
 
function isbn_sum($isbn, $len) 
{ 
 
$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) 
{ 
 
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) 
{ 
 
$len = strlen($isbn); 
if ($len!=10 && $len!=13) 
return 0; 
$rc = isbn_compute($isbn, $len); 
if ($isbn[$len-1] != $rc)  
return 0; 
else 
return 1; 
} 

After the function is written, it can be directly called, the following is the call example:
 
<?php echo is_isbn('9787507421781') ? ' Check by ' : ' Check failure '; ?> 

I wrote another online tool to check the ISBN using the tool can verify the legitimacy of the ISBN code online, you can click the following link using: (link: http://www.ludou.org/tool/isbn/)

Related articles: