PHP实现把数字ID转字母ID


Posted in PHP onAugust 12, 2013

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

   alphaID(12354);  //会将数字转换为字母。
   alphaID('PpQXn7COf',true);//会将字母ID转换为对应的数字。
   alphaID(12354,false,6);//指定生成字母ID的长度为6.

源码:
<?php
/**
 * 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 will
 * translate back e.g.:
 * PpQXn7COf --> 9007199254740989
 *
 * this function is 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 be at least 3 letter long, use the
 * $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. The
 * // 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 <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 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    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  <<--  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;
}
PHP 相关文章推荐
解决GD中文乱码问题
Feb 14 PHP
MayFish PHP的MVC架构的开发框架
Aug 13 PHP
用Zend Encode编写开发PHP程序
Feb 21 PHP
探讨多键值cookie(php中cookie存取数组)的详解
Jun 06 PHP
PHP return语句另类用法不止是在函数中
Sep 17 PHP
PHP把MSSQL数据导入到MYSQL的方法
Dec 27 PHP
php调用KyotoTycoon简单实例
Apr 02 PHP
PHP请求远程地址设置超时时间的解决方法
Oct 29 PHP
php获取数据库中数据的实现方法
Jun 01 PHP
PHP在同一域名下两个不同的项目做独立登录机制详解
Sep 22 PHP
php传值和传引用的区别点总结
Nov 19 PHP
PHP扩展安装方法步骤解析
Nov 24 PHP
PHP计算2点经纬度之间的距离代码
Aug 12 #PHP
php读取csv文件后,uft8 bom导致在页面上显示出现问题的解决方法
Aug 10 #PHP
浅析php中常量,变量的作用域和生存周期
Aug 10 #PHP
浅析linux下apache服务器的配置和管理
Aug 10 #PHP
本地机apache配置基于域名的虚拟主机详解
Aug 10 #PHP
php 删除目录下N分钟前创建的所有文件的实现代码
Aug 10 #PHP
php中如何判断一个网页请求是ajax请求还是普通请求
Aug 10 #PHP
You might like
php 文件上传系统手记
2009/10/26 PHP
PHP使用数组实现队列
2012/02/05 PHP
php中unlink()、mkdir()、rmdir()等方法的使用介绍
2012/12/21 PHP
PHP字符串的递增和递减示例介绍
2014/02/11 PHP
php使用pack处理二进制文件的方法
2014/07/03 PHP
Yii框架 session 数据库存储操作方法示例
2019/11/18 PHP
javascript字符串拼接的效率问题
2010/12/25 Javascript
简短几句jquery代码的实现一个图片向上滚动切换
2011/09/02 Javascript
javascript 进阶篇2 CSS XML学习
2012/03/14 Javascript
document.createElement()用法
2013/03/13 Javascript
jquery教程限制文本框只能输入数字和小数点示例分享
2014/01/13 Javascript
jquery实现点击弹出层效果的简单实例
2014/03/03 Javascript
jQuery事件之键盘事件(ctrl+Enter回车键提交表单等)
2014/05/11 Javascript
原生javascript获取元素样式
2014/12/31 Javascript
解决js图片加载时出现404的问题
2020/11/30 Javascript
js实现无缝滚动特效
2015/12/20 Javascript
详解jQuery中的empty、remove和detach
2016/04/11 Javascript
javascript深拷贝(deepClone)详解
2016/08/24 Javascript
JS正则替换掉小括号及内容的方法
2016/11/29 Javascript
微信小程序scroll-view仿拼多多横向滑动滚动条
2020/04/21 Javascript
Node.js Event Loop各阶段讲解
2019/03/08 Javascript
vue滚动tab跟随切换效果
2020/06/29 Javascript
nodemon实现Typescript项目热更新的示例代码
2019/11/19 Javascript
Python使用PIL库实现验证码图片的方法
2016/03/11 Python
Python实现将不规范的英文名字首字母大写
2016/11/15 Python
python使用knn实现特征向量分类
2018/12/26 Python
python 装饰器的实际作用有哪些
2020/09/07 Python
DELPHI面试题研发笔试试卷
2015/11/08 面试题
大学辅导员事迹材料
2014/02/05 职场文书
小学生倡议书范文
2014/05/13 职场文书
广播体操口号
2014/06/18 职场文书
机关作风建设整改方案
2014/10/27 职场文书
股权转让协议范本
2014/12/07 职场文书
婚礼答谢词
2015/01/04 职场文书
市语委办2016年第十九届“推普周”活动总结
2016/04/05 职场文书
导游词之韩国济州岛
2019/10/28 职场文书