ISBN Converter Source Code

Below is the source code for the ISBN Converter.  For those without the Website Baker CMS, you can copy and paste this code into a .php file and uncomment the opening and closing php tags.

/* Opening php tag i.e. <?php not necessary because this is a code page */

//<?php

//    ISBN Functions
//    Compiled by Adam Reineke (Jan 12, 2007)
//        from original code and comments posted on
//        http://www.blyberg.net/2006/04/05/php-port-of-isbn-1013-tool/
 
function isbn_convert($isbn) {
        $isbn2 = substr("978" . trim($isbn), 0, -1);
        $sum13 = isbn_genchksum13($isbn2);
        $isbn13 = "$isbn2$sum13";
        return ($isbn13);
    }
 
function isbn_gettype($isbn) {
        $isbn = trim($isbn);
        if (preg_match('%[0-9]{12}?[0-9Xx]%s', $isbn)) {
            if (isbn_validatettn($isbn)==1){
                    return 13;
                       } else {return(-1);}
 
                } else if (preg_match('%[0-9]{9}?[0-9Xx]%s', $isbn)) {
              if (isbn_validateten($isbn)==1){
                    return 10;
                       } else {return -1;}
        } else {
            return -1;
        }
    }
 
function isbn_validateten($isbn) {
        $isbn = trim($isbn);
        $chksum = substr($isbn, -1, 1);
        $isbn = substr($isbn, 0, -1);
        if (preg_match('/X/i', $chksum)) { $chksum="10"; }
        $sum = isbn_genchksum10($isbn);
        if ($chksum == $sum){
            return 1;
        }else{
            return 0;
        }
    }
 
function isbn_validatettn($isbn) {
        $isbn = trim($isbn);
        $chksum = substr($isbn, -1, 1);
        $isbn = substr($isbn, 0, -1);
        if (preg_match('/X/i', $chksum)) { $chksum="10"; }
        $sum = isbn_genchksum13($isbn);
        if ($chksum == $sum){
            return 1;
        }else{
            return 0;
        }
    }
 
function isbn_genchksum13($isbn) {
    $t = 2;
    $isbn = trim($isbn);
    $b=0;
    for($i = 1; $i <= 12; $i++){
        $c = substr($isbn,($i-1),1);
        if ($i % 2==0){
            $a = (3 * $c);
        } else {
            $a = (1 * $c);
        }
    $b=$b+$a;
    }
    $sum = 10 - ($b % 10);
    if($sum == 10) $sum = 0;
    return $sum;
    }
 
function isbn_genchksum10($isbn) {
    $t = 2;
    $isbn = trim($isbn);
    $b=0;
    for($i = 0; $i < 9; $i++){
       $c = substr($isbn,$i,1);
       $a = (($i+1) * $c);
       $b=$b+$a;
    }
    $sum = ($b % 11);
    return $sum;
    }

//Clean out the dashes and spaces from passed ISBN
function cleandashes($isbn){
    return $isbn = str_replace(array('-', ' '), '', $isbn);
    }

//Convert ISBN-13 back into ISBN-10
function convert13($isbn) {
    $isbn2 = substr("" . trim($isbn), 3, 9);
    $sum10 = isbn_genchksum10($isbn2);
    if ($sum10==10) {$sum10='X';}
    $isbn10 = $isbn2.$sum10;
    return ($isbn10);
    }
       
## ISBN Add Dashes
function isbn_dashes($isbn) {
    switch(strlen($isbn)):
        case 13:
            return
                substr($isbn,0,3)."-"
                .substr($isbn,3,1)."-"
                .substr($isbn,4,3)."-"
                .substr($isbn,7,5)."-"
                .substr($isbn,12,1);
            break;
        case 10:
            return
                substr($isbn,0,1)."-"
                .substr($isbn,1,3)."-"
                .substr($isbn,4,5)."-"
                .substr($isbn,9,1);
            break;
        default: return false; break;
    endswitch;
    }

if(!isset($_POST['isbn_list'])){
$output = "";
$output .= "<script type='text/javascript'>
function validate()
{
x=document.isbn_convert

if (x.isbn_list.value==\"\")
{
alert(\"Please enter at least one ISBN\")
return false;
}
if (x.isbn_list.value.replace(/[\s]/g,\"\")==\"\")
{
alert(\"Please enter at least one ISBN\")
return false;
}

return true;
}
</script>";

$output .= "<p style='width:300px; font-weight:bold; font-size: 14px;'>Instructions:</p>";
$output .= "<p style='width:300px; font-size: 12px;'>Paste into the text box the list of ISBN's that you would like converted and check the desired conversion method.</p>";
echo "<form name='isbn_convert' action='". $_SERVER['PHP_SELF'] ."' method='post' style='margin: 0;' onsubmit='return validate()'>";
$output .= "<table style='width: 300px;'>";
$output .= "<tr>";
$output .= "<td colspan='2'><textarea name='isbn_list' style='width: 100%; height:300px;'>";
$output .= "</textarea></td></tr>";
$output .= "<tr><td colspan='2'>Convert from ISBN-10 to ISBN-13: ";
$output .= "<input type='radio' name='isbn_output' id='isbn_13' value='0' checked='checked' />";
$output .= "</td></tr><tr><td colspan='2' style='padding-bottom: 10px;'>Convert from ISBN-13 to ISBN-10: ";
$output .= "<input type='radio' name='isbn_output' id='isbn_10' value='1' />";
$output .= "</td></tr>";
$output .= "<tr><td style='text-align:left;'><input type='reset' name='reset' value='Reset Form'></td><td style='text-align:right;'><input type='submit' value=\"Convert ISBN's\"></td></tr>";
$output .= "</table>";
$output .= "</form>";
echo $output;
}

if(isset($_POST['isbn_list']) && isset($_POST['isbn_output'])){
$isbn_list = $_POST['isbn_list'];
$isbn_ar = array();
$isbn_ar = preg_split("/[\s,]+/",$isbn_list);
$isbn_output= $_POST['isbn_output'];

$output = "";

if($isbn_list == ""){
$output .= "You must enter at least one ISBN.";
exit;
}

if($isbn_output== "0"){
    $isbn_output = "isbn_convert";
    $header1 = "ISBN-10";
    $header2 = "ISBN-13";
    $isbn_valid = "10";
    }else{
    $isbn_output = "convert13";
    $header1 = "ISBN-13";
    $header2 = "ISBN-10";
    $isbn_valid = "13";
}

$output .= "<p style='width:400px; font-weight:bold; font-size: 14px;'>Conversion Complete!</p>";
$output .= "<p style='width:400px; font-size: 12px;'>Your  converted ISBN's are below.  The first column contains the ISBN's you requested to have converted, and the second column contains the converted ISBN's.  Any ISBNS that were not in the correct format were not converted and will be marked in red.</p>";
$output .= "<p style='width:400px; font-size: 12px;'>You can copy the column you need by positioning your cursor at the first character of the first row, left-clicking your mouse, and dragging down to the bottom of the list.</p>";
$output .= "<input type='button' value='Back' onClick='javascript: window.location = \"". $_SERVER['PHP_SELF'] ."\"' style='width: 60px; margin-top: 5px;' />";



$output .= "<table><tr><td style='vertical-align: top;'>";
$output .= "<table><tr><td style='font-weight:bold; font-size: 12px;'>".$header1."</td>";
for ($a = 0,$b = sizeof($isbn_ar);$a < $b;$a++) {
    $output .= "<tr><td>".$isbn_ar[$a]."</td></tr>";   
}

$output .= "</table>";
$output .= "</td><td style='vertical-align: top;'>";
$output .= "<table><tr><td style='font-weight:bold; font-size: 12px;'>".$header2."</td>";
for ($a = 0,$b = sizeof($isbn_ar);$a < $b;$a++) {
  $isbn_type =   isbn_gettype($isbn_ar[$a]);
  if($isbn_type == $isbn_valid) {
    $output .= "<tr><td>".$isbn_output($isbn_ar[$a])."</td></tr>";
    } else {
        if($isbn_ar[$a] == ""){
        $output .= "<tr><td>&nbsp;</td></tr>";
        }else{
        $output .= "<tr><td style='background-color: red'>Invalid Input</td></tr>";
        }
    }
}

$output .= "</table>";
$output .= "</td></tr></table>";
$output .= "<input type='button' value='Back' onClick='javascript: window.location = \"". $_SERVER['PHP_SELF'] ."\"' style='width: 60px; margin-top: 5px;' />";
echo $output;
}

//?>

/* Closing php tag i.e. ?> not necessary because this is a code page */