php 数字序列,在PHP中将整数转换为字母数字序列

convertIntToShortCode($_GET["id"]); // Test codes

function convertIntToShortCode($id) {

$chars = "123456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";

$id = intval($id);

if ($id < 1) {

echo "ERROR1";

}

$length = strlen($chars);

// make sure length of available characters is at

// least a reasonable minimum - there should be at

// least 10 characters

if ($length < 10) {

echo "ERROR2";

}

$code = "";

while ($id > $length - 1) {

// determine the value of the next higher character

// in the short code should be and prepend

$code = $chars[fmod($id, $length)] . $code;

// reset $id to remaining value to be converted

$id = floor($id / $length);

}

// remaining value of $id is less than the length of

// self::$chars

$code = $chars[$id] . $code;

echo $code;

}

?>

虽然它有效,但我的一些数字(数据库ID)输出了奇怪的短代码:

1 - > 2 2 - > 3 ... 10 - > c 11 - > d 12 - > e ...

有没有简单的方法可以修改这段代码,这样我生成的短代码只比一个字符长(每个短代码至少有两三个字符),即使对于1,2,3等整数也是如此?

还有谁能告诉我,上面这个算法如何输出整数的短代码?

提前致谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值