PHP 实现把数字 ID 转字母 ID
以下一览
php 是对使用 PHP 把数字 ID 转字母 ID 的实现代码进行了详细的分析介绍,需要
的朋友可以过来参考下
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:#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.
*