求帮忙把这个JS翻译为PHP版本

逍遥混世 2015-02-21 08:45:45
function  uniqid(prefix, more_entropy) {
if (typeof prefix == 'undefined') {
prefix = "";
}

var retId;
var formatSeed = function (seed, reqWidth) {
seed = parseInt(seed, 10).toString(16); // to hex str
if (reqWidth < seed.length) { // so long we split
return seed.slice(seed.length - reqWidth);
}
if (reqWidth > seed.length) { // so short we pad
return Array(1 + (reqWidth - seed.length)).join('0') + seed;
}
return seed;
};

if (!this.php_js) {
this.php_js = {};
}
if (!this.php_js.uniqidSeed) { // init seed with big random int
this.php_js.uniqidSeed = Math.floor(Math.random() * 0x75bcd15);
}
this.php_js.uniqidSeed++;

retId = prefix; // start with prefix, add current milliseconds hex string
retId += formatSeed(parseInt(new Date().getTime() / 1000, 10), 8);
retId += formatSeed(this.php_js.uniqidSeed, 5); // add seed hex string
if (more_entropy) {
// for more entropy we add a float lower to 10
retId += (Math.random() * 10).toFixed(8).toString();
}

return retId;
}
...全文
80 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
傲雪星枫 2015-02-21
  • 打赏
  • 举报
回复
php自带就有这个方法,不需要自己写,而你的方法,就是用php的方法转用js写的。

uniqid — 生成一个唯一ID

说明
string uniqid ([ string $prefix = "" [, bool $more_entropy = false ]] )


<?php
echo uniqid();
?>


http://www.w3school.com.cn/php/func_misc_uniqid.asp

21,873

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧