linein designs

05 Jan, 2007

Convert ISBN10 to ISBN13 with PHP

Posted by: john In: php|programing

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:

3 Comments »

 
Comment by PASH
 
Comment by Bob Zipoli
 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Trackback responses to this post



Please Support Our Friends

Send emails with ease on this SMTP server by JangoSmtp.com.

Banner

About

My name is John Veldboom and have been in design and web programing for just over 8 years now. Started off as a hobby in high school but it has developed into a full time job now. I always tell people that if I wasn't doing this at work each day, I would be at home doing it for free. I love it!

Please visit the contact page to drop me a message.