如何通过使用base_convert从行id(pk)生成Randome字符串 [英] How to generate Randome string from row id(pk) by using base_convert

查看:141
本文介绍了如何通过使用base_convert从行id(pk)生成Randome字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的表

----Id----Product----RandomKey--
|   1   |   P1     |  (some key) |
----------------------------------
|   2   |   P2     |  (some key) |
----------------------------------
|   3   |   P3     |  (some key) |
----------------------------------

这里的某些键是唯一的随机键

我想从base_convert生成随机字符串.均值检查前一个id假设为3,然后将该值加1,然后使用base_convert进行转换

I want to generate randome string from base_convert. Mean check the previous id suppose 3 and then increment 1 with the value and convert it with using base_convert

$rowid=3; //Get the last id from database 
$bnum = base_convert($rowid,36,10);
$rkey = base_convert($bnum+1,10,36);
echo $rkey;

它是否总是生成随机唯一密钥?我真的不理解 formbase tobase base_convert 语法

Does it always generate randome unique key? I really don't understand formbase and tobase in base_convert syntax

base_convert(number,frombase,tobase);

base_convert(number,frombase,tobase);

任何人都可以向我解释 frombase tobase 的工作方式,并且可以通过这种方法生成唯一密钥吗?

Can any one explain me how frombase and tobase work and by this method can i generate unique key?

更新:

如果我设置了

$ rkey = base_convert($ bnum + 1,10,36);

$rkey = base_convert($bnum+1,10,36);

它将生成固定数字,我想要带字符的数字.所以我将 36 更改为 16 表示十六进制格式

It will generate flat number, i want number with character. So I changed 36 to 16 mean Hexadecimal like

$ rkey = base_convert($ bnum + 1,10,16);

$rkey = base_convert($bnum+1,10,16);

,它将产生带有数字和字符的随机字符串.如果有人推荐更好的东西,请告诉我.谢谢

and it will produce random string with number and character. If anyone recommend something better please let me know. Thanks

推荐答案

任何人都可以解释一下frombase和tobase的工作原理,并且可以通过这种方法生成唯一密钥吗?

Can any one explain me how frombase and tobase work and by this method can i generate unique key?

函数 base_convert 不提供唯一字符串的生成器.使用此功能的目的是在数字系统之间进行转换.

Function base_convert do not provide generator for unique strings. The purpose of using this function is converting between numeral systems.

示例:

$randInt = 10; // we pick up a random integer value
$convertedRandInt = base_convert($randInt, 10, 2); // we converting it

var_dump($convertedRandInt); // output: 00001010

在上面的示例中,我使用此函数在十进制数字系统和二进制系统之间进行转换.

In above example, I used this function to convert between decimal numeral system to binary.

string base_convert ( string $number , int $frombase , int $tobase )

第一个参数是字符串或数字(因为char具有数字符号,第二个是变量的当前数字系统(在示例中,我使用了十进制),第三个参数是我们要查找的数字系统(在示例中,二进制).

Fist parameter is string or number (because chars has their numeric sign, the second is current numeral system of variable (in the example I used decimal) and the third argument is numeral system what we are looking for (in the example, binary).

如果有人推荐更好的东西,请告诉我.谢谢

If anyone recommend something better please let me know. Thanks

关于唯一性,请查看旧主题的答案: PHP:如何生成一个随机的,唯一的字母数字字符串?.

About uniqueness, look at the answers on older topic: PHP: How to generate a random, unique, alphanumeric string?.

这篇关于如何通过使用base_convert从行id(pk)生成Randome字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆