I've been writing a textbook buyback system for a buddy of mine. He runs a service that purchases textbooks from students at the end of a school semester. He wanted me to write a program to query Amazon.com and estimate the best value to offer a student for his or her book.
As of January 1, 2007, the agency that controls ISBN codes for all books printed in the world has changed their code format from 10 digits (regular expression /[0-9Xx]{10}/) to 13 digits (regular expression /[0-9]{13}/). Amazon has not updated their Web E-Commerce API to accommodate this change in standard. Thus, any time I get an 13-digits ISBN, I must convert it to the older 10-digits standard.
When "Googling" for solutions to this problem, I found several, but I decided to write this one myself.
// Converts ISBN-13 to ISBN-10 // Leaves ISBN-10 numbers (or anything else not matching 13 consecutive numbers) alone function ISBN13toISBN10($isbn) { if (preg_match('/^\d{3}(\d{9})\d$/', $isbn, $m)) { $sequence = $m[1]; $sum = 0; $mul = 10; for ($i = 0; $i < 9; $i++) { $sum = $sum + ($mul * (int) $sequence{$i}); $mul--; } $mod = 11 - ($sum%11); if ($mod == 10) { $mod = "X"; } else if ($mod == 11) { $mod = 0; } $isbn = $sequence.$mod; } return $isbn; }