当前位置:Gxlcms > PHP教程 > PHP把数目字ID转字母ID

PHP把数目字ID转字母ID

时间:2021-07-01 10:21:17 帮助过:6人阅读

PHP把数字ID转字母ID

PHP把数字ID转字母ID

ID是网站中经常出现的,它一般是数字,但是我们发现现在的网站很多ID都是字母了,比如YouTube的视频播放页它的URL类似/watch?v=yzNjIBEdyww。 下面是一个生成字母ID的方法。
使用示例:

  1. echo alphaID(12354); //qnd
  2. echo alphaID('qnd',true); //12354
  3. echo alphaID(12354,false,6); //qndaab
  4. echo alphaID('qndaab',true, 6); //12354

源码:

  1. <!--?php
  2. /**
  3. * Translates a number to a short alhanumeric version
  4. *
  5. * Translated any number up to 9007199254740992
  6. * to a shorter version in letters e.g.:
  7. * 9007199254740989 ----> PpQXn7COf
  8. *
  9. * specifiying the second argument true, it will
  10. * translate back e.g.:
  11. * PpQXn7COf --> 9007199254740989
  12. *
  13. * this function is based on any2dec && dec2any by
  14. * fragmer[at]mail[dot]ru
  15. * see: http://nl3.php.com/manual/en/function.base-convert.php#52450
  16. *
  17. * If you want the alphaID to be at least 3 letter long, use the
  18. * $pad_up = 3 argument
  19. *
  20. * In most cases this is better than totally random ID generators
  21. * because this can easily avoid duplicate ID's.
  22. * For example if you correlate the alpha ID to an auto incrementing ID
  23. * in your database, you're done.
  24. *
  25. * The reverse is done because it makes it slightly more cryptic,
  26. * but it also makes it easier to spread lots of IDs in different
  27. * directories on your filesystem. Example:
  28. * $part1 = substr($alpha_id,0,1);
  29. * $part2 = substr($alpha_id,1,1);
  30. * $part3 = substr($alpha_id,2,strlen($alpha_id));
  31. * $destindir = "/".$part1."/".$part2."/".$part3;
  32. * // by reversing, directories are more evenly spread out. The
  33. * // first 26 directories already occupy 26 main levels
  34. *
  35. * more info on limitation:
  36. * - http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/165372
  37. *
  38. * if you really need this for bigger numbers you probably have to look
  39. * at things like: http://theserverpages.com/php/manual/en/ref.bc.php
  40. * or: http://theserverpages.com/php/manual/en/ref.gmp.php
  41. * but I haven't really dugg into this. If you have more info on those
  42. * matters feel free to leave a comment.
  43. *
  44. * @author Kevin van Zonneveld <kevin@vanzonneveld.net>
  45. * @author Simon Franz
  46. * @author Deadfish
  47. * @copyright 2008 Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  48. * @license http://www.opensource.org/licenses/bsd-license.php New BSD Licence
  49. * @version SVN: Release: $Id: alphaID.inc.php 344 2009-06-10 17:43:59Z kevin $
  50. * @link http://kevin.vanzonneveld.net/
  51. *
  52. * @param mixed $in String or long input to translate
  53. * @param boolean $to_num Reverses translation when true
  54. * @param mixed $pad_up Number or boolean padds the result up to a specified length
  55. * @param string $passKey Supplying a password makes it harder to calculate the original ID
  56. *
  57. * @return mixed string or long
  58. */
  59. function alphaID($in, $to_num = false, $pad_up = false, $passKey = null)
  60. {
  61. $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  62. if ($passKey !== null) {
  63. // Although this function's purpose is to just make the
  64. // ID short - and not so much secure,
  65. // with this patch by Simon Franz (http://blog.snaky.org/)
  66. // you can optionally supply a password to make it harder
  67. // to calculate the corresponding numeric ID
  68. for ($n = 0; $n<strlen($index); $n++)="" {="" $i[]="substr(" $index,$n="" ,1);="" }="" $passhash="hash('sha256',$passKey);" <="" strlen($index))="" ?="" hash('sha512',$passkey)="" :="" $passhash;="" for="" ($n="0;" $n="" strlen($index);="" $p[]="substr($passhash," 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;="" ($t="0;" $t="" $t++)="" $bcpow="bcpow($base," $t);="" +="" strpos($index,="" substr($in,="" $t,="" 1))="" *="" $bcpow;="" (is_numeric($pad_up))="" $pad_up--;="" ($pad_up=""> 0) {
  69. $out -= pow($base, $pad_up);
  70. }
  71. }
  72. $out = sprintf('%F', $out);
  73. $out = substr($out, 0, strpos($out, '.'));
  74. } else {
  75. // Digital number -->> alphabet letter code
  76. if (is_numeric($pad_up)) {
  77. $pad_up--;
  78. if ($pad_up > 0) {
  79. $in += pow($base, $pad_up);
  80. }
  81. }
  82. $out = "";
  83. for ($t = floor(log($in, $base)); $t >= 0; $t--) {
  84. $bcp = bcpow($base, $t);
  85. $a = floor($in / $bcp) % $base;
  86. $out = $out . substr($index, $a, 1);
  87. $in = $in - ($a * $bcp);
  88. }
  89. $out = strrev($out); // reverse
  90. }
  91. return $out;
  92. }</strlen($index);></kevin@vanzonneveld.net>


人气教程排行