PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I hope many of you would have heard about the new isbn 13 format. it is going to be used from jan 2007 and in the process of changing existing isbns.
according to isbn site, these are the following steps involved.
Example: 0-123456-47-9
Begin with prefix of “978”
Use the first nine numeric characters of the ISBN (include dashes)
978-0-123456-47-
Calculate the EAN check digit using the “Mod 10 Algorithm”
978-0-123456-47-2
ISBN-10 0-123456-47-9
ISBN-13 978-0-123456-47-2
My doubt is about the mod 10 algorithm on which set of numbers
1. 0-123456-47-9
or
2. 978-0-123456-47
i tried to use this Luhn algorithm from wikipedia, but does not seem to work on all isbns on both of the formats mentioned above.
function checkLuhn(string purportedCC) {
int sum := 0
int nDigits := length(purportedCC)
int parity := nDigits modulus 2
for i from 0 to nDigits - 1 {
int digit := integer(purportedCC[i])
if i modulus 2 = parity
digit := digit × 2
if digit > 9
digit := digit - 9
sum := sum + digit
}
return (sum modulus 10) = 0
}
We can validate whether our conversion is correct or not by using this url from isbn site.
Since the checksum is used to detect incorrect codes, it only makes sense to calculate it on the NEW code.
You already have the checksum on the old one, and the required steps would not speak of it at all if you didn't have to recalc it.
i earlier found a class for converting isbn10 to isbn13 but i could not find code that converts isbn13 to isbn10.
i have googled and i could not find. please let me know if you know or find one.
function isbn10to13($isbn10) {
if(!checkLuhn($isbn10)){
echo 'invalid input isbn';
return false;
}
$isbn13 = '978-'.substr($isbn10,0,-1);
for($i=0;$i<10;$i++){
if(checkLuhn($isbn13.$i)) return($isbn13.$i);
}
echo 'unable to convert the isbn';
return false;
}
function checkLuhn($number) {
// Copyright (c) Richard Warrender. Licenced under the GFDL.
// http://www.vividreflection.com/
// Modified slightly by Kieran Huggins
// http://kieran.ca
$number = preg_replace('/\D/','',$number);
// Get total amount of digits to process
$digitCount = strlen((String) $number);
// Checksum must be zero to begin with
$checksum = 0;
// Loop round card number extracting digits
for($i = 1; $i<=$digitCount; $i++) {
// Extract digit number
$digits[$i] = (int) substr($number, -$i, 1);
// Check to see if this the luhn number, we need to double it
if(($i%2) == 0) {
// Double luhn digit
$digit = $digits[$i] * 2;
// If greater or equal to 10, then use sum of digits
if($digit >= 10) {
// Get first digit
$firstDigit = substr($digit, 0, 1);
// Get second digit
$secondDigit = substr($digit, 1, 1);
/// Add together and replace original luhn digit
$digit = $firstDigit + $secondDigit;
}
// Reload back into array
$digits[$i] = $digit;
}
// Keep a running total for use in checksum
$checksum += $digits[$i];
}
if(($checksum % 10) == 0) {
return true;
} else {
return false;
}
}