From: John Date: Wed, 18 Jul 2018 19:25:24 +0000 (-0500) Subject: add PHP num2code and code2num X-Git-Url: https://jfr.im/git/snippets.git/commitdiff_plain/d2b9dfab63daeebdedf37e00dc0d5d9219a501d4 add PHP num2code and code2num --- diff --git a/snippets.php b/snippets.php new file mode 100644 index 0000000..19bfdee --- /dev/null +++ b/snippets.php @@ -0,0 +1,32 @@ + 0) { + $buf[] = $dict[$num % $base]; + $num = (int)($num/$base); + var_dump($num); + } + return implode(array_reverse($buf)); +} + +function code2num($str, $dict) { + // Source: perl Acme::Tools + // License: GNU GPL v1+; Artistic License + if (is_array($dict)) { + $dict = implode($dict); + } + $base = strlen($dict); + $buf = 0; + foreach (str_split($str) as $c) { + $buf = $buf*$base + strpos($dict, $c); + } + return $buf; +} + +// vim: ts=4 nosmarttab noexpandtab