Title: ISBN verification and formatting functions
Contributor: Jeffrey Paul (sneak)
Last Update: Friday March 31 00:22 EDT 2000
<?php
/*
ISBN Handling PHP header file
3/31/2k
by Jeffrey Paul
This code is released under the terms of the GPL.
NOTE: this is only designed for english-language ISBNs.
functions:
+---------------------------------------------------------+
| int verify_isbn(string isbn)
|
+---------------------------------------------------------+
| Returns 0 on good, -1 on bad, -2 on non-english ISBN.
|
| Input string *must* have "ISBN" as the first four characters or it's
|
| invalid.
|
|
|
| Note: Totally invalid strings (not valid in other countries) may return a
|
| -2 value. This is not a true bug, just a known misfeature.
|
+---------------------------------------------------------+
+---------------------------------------------------------+
| string format_isbn(string isbn, string [delimiter])
|
+---------------------------------------------------------+
| Returns ISBN formatted by delimiter, -1 on a invalid isbn, or -2 on
|
| a non-english ISBN. Everything that isn't 0-9 is stripped from the input,
|
| and then the number is verified. It won't format bad stuff. If only one
|
| argument is specified, delimiter is assumed to be '-' (a dash). Does not
|
| print "ISBN" at the beginning.
|
|
|
| Note: Totally invalid strings (not valid in other countries) may return a
|
| -3 value. This is not a true bug, just a known misfeature.
|
+---------------------------------------------------------+
PS: I'm not anti-(!america), I just don't have spec data on how to properly
parse
ISBNs for other countries. Sorry.
*/
function verify_isbn ($input) {
if(strlen($input) > 20)
return(-1);
if(strtoupper(substr($input,0,4)) != "ISBN")
return(-1);
$potential = ereg_replace("[- ]", "", strtoupper(substr($input,4)));
if(ereg("[^0-9X]", $potential))
return(-1);
if(substr($potential,0,1) != 0 && substr($potential,0,1) != 1)
return(-2);
if(strlen($potential) != 10)
return(-1);
$checkbyte = substr($potential,9,1);
if($checkbyte == "X")
$checkbyte = 10;
//too lazy to for() loop it properly...
$total =
substr($potential,0,1) * 10
+
substr($potential,1,1) * 9
+
substr($potential,2,1) * 8
+
substr($potential,3,1) * 7
+
substr($potential,4,1) * 6
+
substr($potential,5,1) * 5
+
substr($potential,6,1) * 4
+
substr($potential,7,1) * 3
+
substr($potential,8,1) * 2
+
$checkbyte
;
$division = $total / 11;
if(ereg("[.]",$division))
{ return(-1); }
else
{ return(0); }
}
function format_isbn ($input, $delimiter = "-") {
$stripped = ereg_replace("[^0-9X]", "", $input);
if(strlen($stripped) > 10)
return(-1);
$verification = verify_isbn("ISBN" . $stripped);
// spec requires ISBN at beginning to be valid.
// so does my function.
if($verification == -1)
return(-1);
if($verification == -2)
return(-2);
$checkbyte = substr($stripped,9,1);
$gid = substr($stripped,0,1);
if($gid == 1 || gid == 0)
{
$stripped = substr(substr($stripped,0,9),1,8);
//this strips off the country code and checkbyte.
$formatted = $gid . $delimiter;
//start formatted string out with groupid and the delimiter. (ex: "0-")
if($gid == 0) {
if($stripped >= 00000000 && $stripped <= 19999999)
$formatted .= substr($stripped,0,2) . $delimiter .
substr($stripped,2,6);
if($stripped >= 20000000 && $stripped <= 69999999)
$formatted .= substr($stripped,0,3) . $delimiter .
substr($stripped,3,5);
if($stripped >= 70000000 && $stripped <= 84999999)
$formatted .= substr($stripped,0,4) . $delimiter .
substr($stripped,4,4);
if($stripped >= 85000000 && $stripped <= 89999000)
$formatted .= substr($stripped,0,5) . $delimiter .
substr($stripped,5,3);
if($stripped >= 90000000 && $stripped <= 94999999)
$formatted .= substr($stripped,0,6) . $delimiter .
substr($stripped,6,2);
if($stripped >= 95000000 && $stripped <= 99999999)
$formatted .= substr($stripped,0,7) . $delimiter .
substr($stripped,7,1);
}
if($gid == 1) {
if($stripped >= 55000000 && $stripped <= 86979999)
$formatted .= substr($stripped,0,5) . $delimiter .
substr($stripped,5,3);
if($stripped >= 86980000 && $stripped <= 99899999)
$formatted .= substr($stripped,0,6) . $delimiter .
substr($stripped,6,2);
if($stripped >= 99900000 && $stripped <= 99999999)
$formatted .= substr($stripped,0,7) . $delimiter .
substr($stripped,7,1);
}
$formatted .= $delimiter . $checkbyte;
//end formatted string with delimiter and checkbyte. (ex: "-7")
return($formatted);
}
else
{
return(-3);
//covering in case of bugs in verification algo...
}
}
?>
Anyone who wishes to make additions or changes to this
PHP Tip email them to webmaster@linuxguruz.org
This document is Copyright (c) 1999, 2000 by LinuxGuruz
Return to the LinuxGuruz PHP Tutorials Page Return to the LinuxGuruz Main Page
|