Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Adding another answer as the first one is also working, though this one is cleaner.</p> <p>I got rid of BC Math functions. If you're going to work with really large integers, this may not work. Otherwise, this is much cleaner solution:</p> <pre><code>function alphaID($in, $to_num = false, $pad_up = false, $passKey = null) { static $passcache; if(empty($passcache)) $passcache = array(); $index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $i = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'); if (!empty($passKey)) { // Although this function's purpose is to just make the // ID short - and not so much secure, // with this patch by Simon Franz (http://blog.snaky.org/) // you can optionally supply a password to make it harder // to calculate the corresponding numeric ID if(isset($passcache[$passKey])) $index = $passcache[$passKey]; else { if(strlen($passhash = hash('sha256',$passKey)) &lt; strlen($index)) $passhash = hash('sha512',$passKey); $p = str_split($passhash); array_multisort($p, SORT_DESC, $i); $index = implode($i); $passcache = $index; } } $base = strlen($index); if ($to_num) { // Digital number &lt;&lt;-- alphabet letter code // A conversion from base $base to base 10 $out = 0; // End number $shift = 1; // Starting shift $len = strlen($in); // Length of string for ($t = 0; $t &lt; $len; $t++) { $out += strpos($index, $in[$t]) * $shift; // $out is a number form alphabet * base^shift $shift *= $base; // increase shift } if (is_numeric($pad_up)) { $pad_up--; if ($pad_up &gt; 0) { $out -= pow($base, $pad_up); } } } else { // Digital number --&gt;&gt; alphabet letter code if (is_numeric($pad_up)) { $pad_up--; if ($pad_up &gt; 0) { $in += pow($base, $pad_up); } } $out = ""; // A simple conversion from base 10 to base $base while ($in &gt; 0) { $remainder = $in % $base; $in = intval(($in-$remainder)/$base); $out .= $index[$remainder]; } } return $out; } </code></pre> <p>The code is cleaner, and should be faster as well. Now it is much easier to see that this is only conversion from base 10 to base $base (62?) and vica-versa. It does not involve floating point division, thus it does not have the bug mentioned above.</p> <p>If you need multiplying large integers, and so on, this can be implemented this way as well with some bright thinking.</p> <p>Added BC Maths, as you said you need large integers</p> <pre><code>function alphaID($in, $to_num = false, $pad_up = false, $passKey = null) { static $passcache; if(empty($passcache)) $passcache = array(); $index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $i = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'); if (!empty($passKey)) { // Although this function's purpose is to just make the // ID short - and not so much secure, // with this patch by Simon Franz (http://blog.snaky.org/) // you can optionally supply a password to make it harder // to calculate the corresponding numeric ID if(isset($passcache[$passKey])) $index = $passcache[$passKey]; else { if(strlen($passhash = hash('sha256',$passKey)) &lt; strlen($index)) $passhash = hash('sha512',$passKey); $p = str_split($passhash); array_multisort($p, SORT_DESC, $i); $index = implode($i); $passcache = $index; } } $base = strlen($index); if ($to_num) { // Digital number &lt;&lt;-- alphabet letter code // A conversion from base $base to base 10 $out = '0'; // End number $shift = 1; // Starting shift $len = strlen($in); // Length of string for ($t = 0; $t &lt; $len; $t++) { $out = bcadd($out, bcmul(strpos($index, $in[$t]),$shift)); // $out is a number from alphabet * base^shift $shift = bcmul($shift, $base); // increase shift } if (is_numeric($pad_up)) { $pad_up--; if ($pad_up &gt; 0) { $out -= pow($base, $pad_up); } } } else { // Digital number --&gt;&gt; alphabet letter code if (is_numeric($pad_up)) { $pad_up--; if ($pad_up &gt; 0) { $in += pow($base, $pad_up); } } $out = ""; // A simple conversion from base 10 to base $base while ($in &gt; '0') // We're treating integer as a string, so BC math works { $remainder = bcmod($in,$base); $in = bcdiv($in, $base); $out .= $index[$remainder]; } } return $out; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload