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 相关文章推荐
php 数组的创建、调用和更新实现代码
Mar 09 PHP
浅析PHP微信支付通知的处理方式
May 25 PHP
PHP实现发送邮件的方法(基于简单邮件发送类)
Dec 17 PHP
PHP消息队列用法实例分析
Feb 12 PHP
CI操作cookie的方法分析(基于helper类库)
Mar 28 PHP
PHP 5.6.11 访问SQL Server2008R2的几种情况详解
Aug 08 PHP
10个值得深思的PHP面试题
Nov 14 PHP
浅谈php中curl、fsockopen的应用
Dec 10 PHP
Thinkphp3.2简单解决多文件上传只上传一张的问题
Sep 26 PHP
PHP使用PDO访问oracle数据库的步骤详解
Sep 29 PHP
PHP实现微信红包金额拆分试玩的算法示例
Apr 07 PHP
PHP FileSystem 文件系统常用api整理总结
Jul 12 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
图形数字验证代码
2006/10/09 PHP
PHP笔记之:日期函数的使用介绍
2013/04/24 PHP
PHP实现把MySQL数据库导出为.sql文件实例(仿PHPMyadmin导出功能)
2014/05/10 PHP
php单例模式的简单实现方法
2016/06/10 PHP
php array_walk_recursive 使用自定的函数处理数组中的每一个元素
2016/11/16 PHP
ThinkPHP框架实现的微信支付接口开发完整示例
2019/04/10 PHP
禁止F5等快捷键的JS代码
2007/03/06 Javascript
jquery下利用jsonp跨域访问实现方法
2010/07/29 Javascript
基于Jquery的回车成tab焦点切换效果代码(Enter To Tab )
2010/11/14 Javascript
文本框输入时 实现自动提示(像百度、google一样)
2012/04/05 Javascript
在javascript中实现函数数组的方法
2013/12/25 Javascript
node.js操作mongoDB数据库示例分享
2014/11/26 Javascript
Bootstrap中表单控件状态(验证状态)
2016/08/04 Javascript
jQuery实现select模糊查询(反射机制)
2017/01/14 Javascript
详解webpack介绍&amp;安装&amp;常用命令
2017/06/29 Javascript
利用vue组件自定义v-model实现一个Tab组件方法示例
2017/12/06 Javascript
jQuery实现带右侧索引功能的通讯录示例【附源码下载】
2018/04/17 jQuery
vue中接口域名配置为全局变量的实现方法
2018/09/20 Javascript
[01:48]DOTA2 2015国际邀请赛中国区预选赛第二日战报
2015/05/27 DOTA
Python守护进程(daemon)代码实例
2015/03/06 Python
用PyQt进行Python图形界面的程序的开发的入门指引
2015/04/14 Python
python实现操作文件(文件夹)
2019/10/31 Python
tensorflow estimator 使用hook实现finetune方式
2020/01/21 Python
Python GUI库PyQt5样式QSS子控件介绍
2020/02/25 Python
关于Python3爬虫利器Appium的安装步骤
2020/07/29 Python
python上下文管理器异常问题解决方法
2021/02/07 Python
CSS实现的一闪而过的图片闪光效果
2014/04/23 HTML / CSS
利用HTML5画出一个坦克的形状具体实现代码
2013/06/20 HTML / CSS
汇智创新科技发展有限公司
2015/12/06 面试题
北大自主招生自荐信
2013/10/19 职场文书
毕业生护理专业个人求职信范文
2014/01/04 职场文书
公司应聘自荐书
2014/06/14 职场文书
商务经理岗位职责
2014/08/03 职场文书
语文教师求职信范文
2015/03/20 职场文书
有关浪费资源的建议书
2015/09/14 职场文书
小学教师师德培训心得体会
2016/01/09 职场文书