PHP numeric and String ID reciprocal function (similar to Youku ID) _php instance

Source: Internet
Author: User
Tags numeric pow strlen

I don't know if you noticed. No, like Youku, Tencent video and other video links seem like this

Copy Code code as follows:

Http://v.youku.com/v_show/id_XNjA5MjE5OTM2.html

Pay attention to id_xxx that paragraph, is not understand, but you can not deny that this is the ID, this is not a cow abroad in 09 to write on the php/python/javascript/java/sql of the generation method, can be seen how I am now outdated, the following I posted the code, Hope to share the spirit forever.

Copy Code code as follows:

<?php
/**
* @author Kevin van Zonneveld <kevin@vanzonneveld.net>
* @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 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 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 < 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;
}

Use examples

Copy Code code as follows:
<?php
Alphaid (9007199254740989);

The execution result will be returned to "FE2XNNGPF" and we can think of it as encryption, and the reverse decryption
Copy Code code as follows:
<?php
Alphaid (' fe2xnngpf ', true);

Then convert it into the real number "9007199254740989". Method can also support the use of key encryption, so that others can not solve your real ID.

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.