3v4l.org

run code in 300+ PHP versions simultaneously
<?php function base62encode($data) { $outstring = ''; $l = strlen($data); for ($i = 0; $i < $l; $i += 8) { $chunk = substr($data, $i, 8); $outlen = ceil((strlen($chunk) * 8)/6); //8bit/char in, 6bits/char out, round up $x = bin2hex($chunk); //gmp won't convert from binary, so go via hex $w = gmp_strval(gmp_init(ltrim($x, '0'), 16), 62); //gmp doesn't like leading 0s $pad = str_pad($w, $outlen, '0', STR_PAD_LEFT); $outstring .= $pad; } return $outstring; } /** * Decode base-62 encoded text into binary * Note that because base-62 encodes slightly less than 6 bits per character (actually 5.95419631038688), there is some wastage * In order to make this practical, we chunk in groups of up to 11 input chars, which give up to 8 output chars * with a wastage of up to 4 bits per chunk, so while the output is not quite as space efficient as a * true multiprecision conversion, it's orders of magnitude faster * Note that the input of this function is not compatible with that of a multiprecision conversion, but it's a practical encoding implementation * The encoding overhead tends towards 37.5% with this chunk size; bigger chunk sizes can be slightly more space efficient, but may be slower * Base-64 doesn't suffer this problem because it fits into exactly 6 bits, so it generates the same results as a multiprecision conversion * Requires PHP 5.3.2 and gmp 4.2.0 * @param string $data Base-62 encoded text (not chunked or split) * @return string Decoded binary data */ function base62decode($data) { $outstring = ''; $l = strlen($data); for ($i = 0; $i < $l; $i += 11) { $chunk = substr($data, $i, 11); $outlen = floor((strlen($chunk) * 6)/8); //6bit/char in, 8bits/char out, round down $y = gmp_strval(gmp_init(ltrim($chunk, '0'), 62), 16); //gmp doesn't like leading 0s $pad = str_pad($y, $outlen * 2, '0', STR_PAD_LEFT); //double output length as as we're going via hex (4bits/char) $outstring .= pack('H*', $pad); //same as hex2bin } return $outstring; } $data = "?1,2,3,45"; $encode = base62encode($data); echo 'Encode: '.$encode; echo ' - '.base62decode($encode);
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.32, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Encode: 5QMw8ONQTfo0r - ?1,2,3,45
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Encode: 5QMw8ONQTfo0r - ?1,2,3,45
Output for 7.3.32 - 7.3.33, 7.4.33, 8.0.13
Fatal error: Uncaught Error: Call to undefined function gmp_strval() in /in/n2XUX:10 Stack trace: #0 /in/n2XUX(43): base62encode('?1,2,3,45') #1 {main} thrown in /in/n2XUX on line 10
Process exited with code 255.

preferences:
234.4 ms | 402 KiB | 330 Q