Created
September 15, 2021 10:24
-
-
Save Shagshag/e3506054121600588ea3069eb538663a to your computer and use it in GitHub Desktop.
Convert GLS track ID to GLS reference
This file contains 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
<?php | |
// based on https://stackoverflow.com/questions/10471991/convertions-between-decimal-and-base-36#10472259 | |
function convertReferenceToTrackID($number) { | |
$offset = 2721109907456; | |
$base = 36; | |
$in = (string) ($number + $offset); | |
$out = ''; | |
for ($i = strlen($in) - 1; $i >= 0; $i--) { | |
$out = base_convert(bcmod($in, $base), 10, $base) . $out; | |
$in = bcdiv($in, $base); | |
} | |
return strtoupper(preg_replace('/^0+/', '', $out)); | |
} | |
function convertTrackIDToReference($trackID) { | |
$offset = 2721109907456; | |
$base = 36; | |
$out = ''; | |
for ($i = 0, $l = strlen($trackID); $i < $l; $i++) { | |
$x = base_convert(substr($trackID, $i, 1), $base, 10); | |
$out = bcadd(bcmul($out, $base), $x); | |
} | |
$out = (int) $out; | |
return $out - $offset; | |
} | |
var_dump(convertReferenceToTrackID(71642028539)); | |
var_dump(convertTrackIDToReference('ZMZ0E96J')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment