Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@KnightAlex
Last active November 28, 2023 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KnightAlex/0e948d2b00291888a8cb8ff08661704c to your computer and use it in GitHub Desktop.
Save KnightAlex/0e948d2b00291888a8cb8ff08661704c to your computer and use it in GitHub Desktop.
ISBN 13 to 10 conversion, via shortcode
add_shortcode('isbn-13to10', 'ISBN13toISBN10_shortcode');
// Converts ISBN-13 to ISBN-10
// Leaves ISBN-10 numbers (or anything else not matching 13 consecutive numbers) alone
function ISBN13toISBN10_shortcode($atts) {
extract( shortcode_atts( array(
'isbn' => ''
), $atts ) );
if (preg_match('/^\d{3}(\d{9})\d$/', $isbn, $m)) {
$sequence = $m[1];
$sum = 0;
$mul = 10;
for ($i = 0; $i < 9; $i++) {
$sum = $sum + ($mul * (int) $sequence[$i]);
$mul--;
}
$mod = 11 - ($sum%11);
if ($mod == 10) {
$mod = "X";
}
else if ($mod == 11) {
$mod = 0;
}
$isbn = $sequence.$mod;
}
return $isbn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment