Skip to content

Instantly share code, notes, and snippets.

@r15ch13
Last active December 16, 2015 15:59
Show Gist options
  • Save r15ch13/5459442 to your computer and use it in GitHub Desktop.
Save r15ch13/5459442 to your computer and use it in GitHub Desktop.
Calculate ISBN checksum
<?php
/**
* Calculate ISBN checksum
*
* @param string $isbn
* @return integer
*/
function isbn_checksum($isbn) {
$sum = 0; $isbn = str_split(preg_replace('/[^\d]/', '', $isbn));
foreach($isbn as $key => $z) {
if($key >= 12) break;
$sum += ($key % 2) ? $z * 3 : $z;
}
$checksum = (10 - $sum % 10);
return ($checksum == 10) ? 0 : $checksum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment