编程语言
首页 > 编程语言> > 什么样的字符集是php的uniqid组成的?

什么样的字符集是php的uniqid组成的?

作者:互联网

我想为php的uniqid准备简单的正则表达式.我检查了uniqid手册,寻找用作返回值的字符集.但文档只提到:

@return string the unique identifier, as a string.

With an empty prefix, the returned string will be 13 characters long. If more_entropy is true, it will be 23 characters.

我想知道在返回值中我可以期待什么字符.它是十六进制字符串吗?如何确定?哪里可以找到更多关于uniqid函数的内容?

解决方法:

文档没有指定字符串内容;只有它的长度.一般来说,你不应该依赖它.如果在一对分隔符(如引号)之间打印值,则可以在正则表达式中使用它们:

"([^"]+)"    ($1 contains the value)

只要您为特定的PHP版本开发,就可以检查它的实现并假设它不会改变.如果升级,则应检查假设是否仍然有效.

A comment in uniqid documentation描述了它本质上是一个带有可选数字后缀的十六进制数字:

if (more_entropy) {
    uniqid = strpprintf(0, "%s%08x%05x%.8F", prefix, sec, usec, php_combined_lcg() * 10);
} else {
    uniqid = strpprintf(0, "%s%08x%05x", prefix, sec, usec);
}

这为您提供了两种可能的输出格式:

> uniqid() – 13个字符,hexadecimal number
> uniqid(”,true) – 14 – 23个字符,hexadecimal number with floating number suffix
computed elsewhere

如果使用除字母数字字符和点之外的其他分隔符,则可以使用这些简单正则表达式之一来获取以下两种格式之一的值:

> [0-9a-f]
> [.0-9a-f]

如果您需要100%格式保证任何PHP版本,您可以编写自己的功能基于sprintf.

我承认,uniqid不太可能会发生重大变化;我希望创建其他扩展来提供不同的格式. Another comment in uniqid documentation显示了符合RFC 4211的UUID实现.还有一个讨论on stackoverflow about it.

标签:php,uniqueidentifier
来源: https://codeday.me/bug/20190627/1308726.html