PHP port of ISBN-10/13 tool

OSS4Lib and David Bigwood (Catalogablog) wrote the other day about an ISBN tool from ManaSystems that converts ISBN-10 to ISBN-13. ManaSystem’s version is written in Perl, so I went ahead and ported it to PHP5 for the rest of us.

Feel free to download it here, or from my files section.

Basically, all the function names are the same, so the original instructions apply. From the ISBN.pl page:

convert($isbn)
Takes a 10 digit ISBN and returns the 13 digit equivalent. Does not perform any error checking or validation.

gettype($isbn)
Takes a string value and will make a guess as to whether or not it fits the criteria of an ISBN. Returns 10 for a possible ISBN-10 and 13 for ISBN-13. Does not validate further.

validateten($isbn)
Takes a 10 digit numeric value and checks to determine if it is a valid ISBN-10.

validatettn($isbn)
Takes a 13 digit digit numeric value and checks to determine if it is a valid ISBN-13.

genchksum13($isbn)
Takes a 12 digit numeric value and generates an ISBN-13 checksum digit.

genchksum10($isbn)
Takes a 9 digit numeric value and generates an ISBN-10 checksum digit.

printinvalid() Returns a message informing the user the ISBN is invalid.

Of course, you’ll instantiate it a little differently:

<?php
require_once('ISBN.php');
 
$isbn_no = "012345678";
$ISBN = new ISBN;
$isbntype = $ISBN->gettype($isbn_no);
 
if ($isbntype < 1) { $ISBN->printinvalid(); }
else if ($isbntype == 10) { $isvalidten = $ISBN->validateten($isbn_no); }
else if ($isbntype == 13) { $isvalidttn = $ISBN->validatettn($isbn_no); }
 
if ($isvalidten) { print "That is a valid ISBN-10\n"; } 
else if ($isvalidttn) { print "That is a valid ISBN-13\n"; } 
else { $ISBN->printinvalid(); }
?>

Enjoy!


About this entry