Since at the start of 2007 all books will no longer be using the ISBN10, we needed a way to convert all those ISBN10 numbers to the new ISBN13 (or EAN).
function genchksum13($isbn) {
$isbn = trim($isbn);
for ($i = 0; $i <= 12; $i++) {
$tc = substr($isbn, -1, 1);
$isbn = substr($isbn, 0, -1);
$ta = ($tc*3);
$tci = substr($isbn, -1, 1);
$isbn = substr($isbn, 0, -1);
$tb = $tb + $ta + $tci;
}
$tg = ($tb / 10);
$tint = intval($tg);
if ($tint == $tg) { return 0; }
$ts = substr($tg, -1, 1);
$tsum = (10 - $ts);
return $tsum;
}
function isbn10_to_13($isbn) {
$isbn = trim($isbn);
if(strlen($isbn) == 12){ // if number is UPC just add zero
$isbn13 = '0'.$isbn;}
else
{
$isbn2 = substr("978" . trim($isbn), 0, -1);
$sum13 = genchksum13($isbn2);
$isbn13 = "$isbn2$sum13";
}
return ($isbn13);
}
// usage //
echo isbn10_to_13('1234567890'); // returns ISBN13
There are no checks in the code to see if the number is numeric for example, but this should give you a good place to start.
Similar Posts:
- Find Time Between Two Dates in PHP
- UPDATED: Strip Off Characters from String Using PHP (substr)
- MySQL Return Single Data or Single Array with PHP
- Find Years and Months Between Two Dates in PHP
- Turn 2D Array into a HTML Dropdown with PHP

October 8th, 2008 @ 5:45 pm
Thank you!!
February 12th, 2009 @ 3:34 am
thank you very much ..
April 26th, 2010 @ 1:54 pm
Excellent – thank you!