<?php
/**
* Created by Phpstorm.
* User:administrator
* DATE:2016/11/1
* time:12:26
*/
/* Converts numbers to character correspondence parsing
* @param mixed $in String or long input to translate
* @param boolean $to _num reverses translation when True
* @param mixed $pad _up number or Boolean padds the result up to a specified length
* @param string $passKey supplying a password makes it harder to calculate the original ID
*/
function Alphaid ($in, $to _num = False, $pad _up = false, $passKey = null)
{
$index = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if ($passKey!== null) {
Although this function ' s purpose was to just make the
ID Short-and isn't so much secure,
With the patch by Simon Franz (http://blog.snaky.org/)
Can optionally supply a password to make it harder
To calculate the corresponding numeric ID
for ($n = 0; $n <strlen ($index); $n + +) {
$i [] = substr ($index, $n, 1);
}
$passhash = hash (' sha256 ', $passKey);
$passhash = (strlen ($passhash) < strlen ($index))
? Hash (' sha512 ', $passKey)
: $passhash;
for ($n =0; $n < strlen ($index); $n + +) {
$p [] = substr ($passhash, $n, 1);
}
Array_multisort ($p, Sort_desc, $i);
$index = implode ($i);
}
$base = strlen ($index);
if ($to _num) {
Digital number <<--alphabet letter code
$in = Strrev ($in);
$out = 0;
$len = strlen ($in)-1;
for ($t = 0; $t <= $len; $t + +) {
$bcpow = Bcpow ($base, $len-$t);
$out = $out + Strpos ($index, substr ($in, $t, 1)) * $BCPOW;
}
if (Is_numeric ($pad _up)) {
$pad _up--;
if ($pad _up > 0) {
$out-= POW ($base, $pad _up);
}
}
$out = sprintf ('%F ', $out);
$out = substr ($out, 0, Strpos ($out, '. '));
} else {
Digital number-->> alphabet letter code
if (Is_numeric ($pad _up)) {
$pad _up--;
if ($pad _up > 0) {
$in + + POW ($base, $pad _up);
}
}
$out = "";
for ($t = Floor (log ($in, $base)); $t >= 0; $t-) {
$BCP = Bcpow ($base, $t);
$a = Floor ($in/$bcp)% $base;
$out = $out. substr ($index, $a, 1);
$in = $in-($a * $bcp);
}
$out = Strrev ($out); Reverse
}
return $out;
}
$str = Alphaid ("1245");
echo $str. " <br/> ";
Echo Alphaid ($str, true);