PHP implementation of the digital ID to the letter id_php tutorial

Source: Internet
Author: User
ID is a frequent occurrence of the site, it is generally a number, but we found that many of the current site ID are letters, such as YouTube's video playback page its URL is similar to/WATCH?V=YZNJIBEDYWW. Here's a way to generate a letter ID.
Examples of Use:
Copy CodeThe code is as follows:
Alphaid (12354);//Converts a number to a letter.
Alphaid (' Ppqxn7cof ', true);//Converts the letter ID to the corresponding number.
Alphaid (12354,false,6);//Specifies the length of the generated letter ID is 6.

Source:
Copy CodeThe code is as follows:
/**
* Translates a number to a short alhanumeric version
*
* Translated any number up to 9007199254740992
* To a shorter version in letters e.g.:
* 9007199254740989-PPQXN7COF
*
* Specifiying The second argument true, it'll
* Translate back e.g.:
* PPQXN7COF-9007199254740989
*
* This function was based on Any2dec && Dec2any by
* Fragmer[at]mail[dot]ru
* see:http://nl3.php.net/manual/en/function.base-convert.php#52450
*
* If you want the Alphaid to is at least 3 letter long, use the
* $pad _up = 3 argument
*
* In most cases better than totally random ID generators
* Because this can easily avoid duplicate ID ' s.
* For example if your correlate the Alpha ID to an auto incrementing ID
* In your database, you ' re-done.
*
* The reverse is do because it makes it slightly more cryptic,
* But it also makes it easier to spread lots of IDs in different
* Directories on your filesystem. Example:
* $part 1 = substr ($alpha _id,0,1);
* $part 2 = substr ($alpha _id,1,1);
* $part 3 = substr ($alpha _id,2,strlen ($alpha _id));
* $destindir = "/". $part 1. " /". $part 2." /". $part 3;
*//By reversing, directories is more evenly spread out. The
*//First directories already occupy-main levels
*
* More info on limitation:
*-http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/165372
*
* If you really need the bigger numbers you probably has to look
* at things like:http://theserverpages.com/php/manual/en/ref.bc.php
* or:http://theserverpages.com/php/manual/en/ref.gmp.php
* But I haven ' t really dugg to this. If you have more info on those
* Matters feel free to leave a comment.
*
* @author Kevin van Zonneveld
* @author Simon Franz
* @author Deadfish
* @copyright Kevin van Zonneveld (http://kevin.vanzonneveld.net)
* @license http://www.opensource.org/licenses/bsd-license.php New BSD Licence
* @version svn:release: $Id: alphaID.inc.php 344 2009-06-10 17:43:59z Kevin $
* @link http://kevin.vanzonneveld.net/
*
* @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
*
* @return Mixed string or long
*/
function Alphaid ($in, $to _num = False, $pad _up = false, $passKey = null)
{
$index = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if ($passKey!== null) {
Although this function ' s purpose are 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

for ($n = 0; $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;
}

http://www.bkjia.com/PHPjc/328184.html www.bkjia.com true http://www.bkjia.com/PHPjc/328184.html techarticle ID is a frequent occurrence of the site, it is generally a number, but we found that many of the current site ID are letters, such as YouTube's video playback page its URL is similar to/WATCH?V=YZNJIBEDYWW. The next ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.