您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP 扩展之根据数字生成唯一的字符串 ID

2024/3/26 9:51:20发布21次查看
本篇文章给大家分享的内容是php 扩展之根据数字生成唯一的字符串 id ,有着一定的参考价值,有需要的朋友可以参考一下
hashids 是一个可以生成唯一的非顺序的字符串 id 号码,它还可以对这些 id 进行解密,你可以利用它来加密你不想暴露给用户的数字 id。安装$ git clone https://github.com/cdoco/hashids.phpc.git $ cd hashids.phpc $ phpize && ./configure && make && make install
你可以设置一些选项在 php.ini 里,或者你也可以在构造方法里面设置,但是我推荐你在 php.ini 中设置,这样你可以拥有更好的性能。
[hashids] extension=hashids.so //默认是空字符串 hashids.salt=cdoco //默认长度是 0 hashids.min_hash_length=20 //默认是 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890 //你可以自己设置它,比如你使用全部小写的字符 hashids.alphabet=abcdefghijklmnopqrstuvwxyz
快速开始$hashids = new hashids(); $hash = $hashids->encode(1, 2, 3, 4, 5); // adf9h9i0sq $numbers = $hashids->decode($hash); // [1, 2, 3, 4, 5] //或者你可以用静态方法调用 $hash = hashids::encode(1, 2, 3, 4, 5); // adf9h9i0sq $numbers = hashids::decode($hash); // [1, 2, 3, 4, 5]
性能原来有纯 php 代码实现的一个功能,现在把它封装成了一个 php 扩展,性能比纯 php 的版本提升了百倍左右
其他$hashids = new hashids(); $hash = $hashids->encode(1, 2, 3, 4, 5); // adf9h9i0sq $hash = $hashids->encode([1, 2, 3, 4, 5]); // adf9h9i0sq
构造方法的参数
new hashids(string $salt, int $min_hash_length, string $alphabet); //example new hashids("this is salt.", 20, 'abcdefghijklmnopqrstuvwxyz');
16 进制加密和解密
$hashids = new hashids(); $hash = $hashids->encodehex('ffffdd'); // rykpak $hex = $hashids->decodehex($hash); // ffffdd
以上就是 php 扩展之根据数字生成唯一的字符串 id 的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product