Check ISBN for Validity

This script accepts an ISBN for input and checks to see if it is a valid number for an ISBN. This doesn't mean that it is an ISBN that has actually been used, just that it could be used as an ISBN. It currently works for ISBNs with 10 digits. I'll expand this soon to include 13 digit ISBNs.

Enter a 10 digit ISBN:

The code, as a function:

function checkisbn10($isbn10) {
   settype($isbn10, 'string');
   $isbn10 = str_replace(array(' ', '-', '.'), '', $isbn10);
   if (strlen($isbn10) != 10) {
      print "ISBN 10 is not 10 digits.<br />\n";
      return false;
   }
   if (!is_numeric(substr($isbn10, -10, 9))) {
      print "ISBN 10 not a number.";
      return false;
   }
   else {
      $checkdigit = substr($isbn10, -1);
      if (!is_numeric($checkdigit)) {
         $checkdigit = strtoupper($checkdigit);
      }
      if ($checkdigit == "X") {
         $checkdigit = 10;
      }
      $sum = 0;
      for ($i = 0; $i < 9; $i++) {
         $sum = $sum + ($isbn10[$i] * (10 - $i));
         }
      $sum = $sum + $checkdigit;
      $mod = $sum % 11;
      if ($mod == 0) {
         return true;
      }
      print "ISBN 10 Bad - check digit doesn't check.<br />\n";
      return false;
   }
   print "ISBN 10 Bad - some other reason. <br />\n";
   return false;
}

This site and its original contents are copyright © 1994-2009 by Erika Stokes.
All original designs belong to the respective copyright owners. All Rights Reserved.