Reputation: 509
I'm trying to make a barcode scanning application for a client and need to differentiate between ISBN and UPC/EAN.
I originally thought ISBN numbers were between 10 or 13 characters long and UPC/EAN were 12 digits long, but reading further the new EAN numbers are 13 digits long!
Since im using 2 API's one for books and one for products i dont want to use all the API requests searching both of them i need a way to differentiate between the two?
I have noticed that on the actual scanning application once the item is scanned it shows weather its a product or book, but i cannot access that part of the scanner as i'm using intel XDK and using the barcode scanner plugin.
Does anyone have any answers on how to use php to find out if the code number is a ISBN or UPC/EAN?
Upvotes: 2
Views: 801
Reputation: 2362
I would use the check-digit. EAN, ISBN, and UPC calculate their check-digits differently so generally I wouldn't expect one to validate against any of the other's.
With that info you can fairly easily write a quick script to validate a given bar code very quickly. Whichever standard it conforms to is likely what it is.
As an example, here is how you would validate an EAN-13 barcode:
function validateEAN13($barcode){
$checksum= 0;
foreach(str_split($barcode) as $i=>$num){ //loop through the barcode
$num = intval($num);
if($i < strlen($barcode) - 1){ //don't include the last digit in the checksum.
$weight = ($i + 1) % 2 == 0 ? 1 : 3;
$checksum += $weight * $num;
}
} //at the end of the loop $num will be equal to the check-digit.
$compare = ( 10 - $checksum % 10);
return ( $num == $compare );
}
You can write similar functions that will validate the other types of bar codes. Then just iterate through each check until it matches something.
EDIT: I just realized that ISBN-13 is a subset of EAN and will validate each other so the above will not work. However, as you pointed out in a response. All ISBNs will start with 978 or 979. No EAN will, since the first 3 digits of an EAN are country of origin and 978/979 belong to "Bookland"
Upvotes: 0
Reputation: 509
As of 2007 ISBNs use 13 digits instead of 10 digits, EAN numbers also use 13 digits,
Too differentiate between the 2 ISBN numbers always start with 978. Thats all i could find and since most of the stock i will be dealing with is from 2007 onwards i havent looked any further into 10 digit ISBN numbers.
Upvotes: 1