Last active
November 28, 2023 10:14
-
-
Save KnightAlex/0e948d2b00291888a8cb8ff08661704c to your computer and use it in GitHub Desktop.
ISBN 13 to 10 conversion, via shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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