V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
gaozihang
V2EX  ›  程序员

一个 PHP 扩展: 根据数字生成唯一的字符串 ID (欢迎 star, 欢迎共同讨论代码 😆)

  •  
  •   gaozihang · 2018-04-13 10:46:29 +08:00 · 1760 次点击
    这是一个创建于 2176 天前的主题,其中的信息可能已经有所发展或是发生改变。

    github: https://github.com/cdoco/hashids.phpc

    一个可以生成类似于 YouTuBe 那种唯一字符串 ID 的功能,可以对用户隐藏数据库中的真正数字 ID

    原来有纯 php 代码实现的一个功能,现在把它封装成了一个 php 扩展,性能比纯 php 的版本提升了百倍左右

    performance comparison

    使用也比较简单:

    $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]
    

    如果你项目中需要这种加密数字 id 的功能,可以试用下这个扩展 😆~

    HowToMakeLove
        1
    HowToMakeLove  
       2018-04-13 11:20:10 +08:00
    有机会试试
    xxxy
        2
    xxxy  
       2018-04-13 17:46:25 +08:00
    请教一下,楼主你的方法跟直接 hash 数字加盐有什么区别?
    gaozihang
        3
    gaozihang  
    OP
       2018-04-13 18:06:24 +08:00
    @xxxy hash 只能 encode,不能 decode 啊,这个可以把你加密的数字再解密出来。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1538 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:03 · PVG 01:03 · LAX 10:03 · JFK 13:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.