PHP implementation to convert the digit ID to the letter ID_PHP tutorial-php Tutorial

Source: Internet
Author: User
PHP converts the digit ID to the letter ID. ID is often seen on websites. it is generally a number, but we find that many of the current website IDs are letters, such as the video play page of YouTube. its URL is similar to watch? VyzNjIBEdyww. The lower ID is often used in websites. it is generally a number, but we find that many of the current website IDs are letters, such as the video playback page of YouTube. its URL is similar to/watch? V = yzNjIBEdyww. The following is a method for generating letter IDs.
Example:

The code is as follows:


AlphaID (12354); // converts a number to a letter.
AlphaID ('ppqxn7cof', true); // converts the letter ID to a corresponding number.
AlphaID (12354, false, 6); // specify that the length of the generated letter ID is 6.


Source code:

The code is as follows:


/**
* Translates a number to a short alhanumeric version
*
* Translate any number up to 9007199254740992
* To a shorter version in letters e.g .:
* 9007199254740989 --> PpQXn7COf
*
* Specifiying the second argument true, it will
* Translate back e.g .:
* PpQXn7COf -- & gt; 9007199254740989
*
* This function is based on any2dec & dec2any
* Fragmer [at] mail [dot] ru
* See: http://nl3.php.net/manual/en/function.base-convert.php#52450
*
* If you want the alphaID to be at least 3 letter long, use
* $ Pad_up = 3 argument
*
* In most cases this is better than totally random ID generators
* Because this can easily avoid duplicate ID's.
* For example if you correlate the alpha ID to an auto incrementing ID
* In your database, you're done.
*
* The reverse is done 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:
* $ Part1 = substr ($ alpha_id, 0, 1 );
* $ Part2 = substr ($ alpha_id, 1, 1 );
* $ Part3 = substr ($ alpha_id, 2, strlen ($ alpha_id ));
* $ Destindir = "/". $ part1. "/". $ part2. "/". $ part3;
* // By reversing, directories are more evenly spread out.
* // First 26 directories already occupy 26 main levels
*
* More info on limitation:
*-Http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/165372
*
* If you really need this for bigger numbers you probably have 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 into 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 2008 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 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 is to just make
// 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;
}

Why? V = yzNjIBEdyww. ...

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.