dongxiai3003 2018-10-27 11:21
浏览 95
已采纳

生成包含预定义字符的5位字母数字代码

I would like to generate a membership number consisting of alphanumeric characters, removing i o and l to save confusion when typing. to be done in php (also using Laravel 5.7 if that matters - but i feel this is a php question)

If simply using 0-9 the membership number would start at 00001 for the 1st one and the 11th person would have 00011. I would like to use alphanumeric characters from 0-9 + a-z (removing said letters)

0-9 (total 10 characters), abcdefghjkmnpqrstuvwxyz (total 23 characters) - this giving a total of 33 characters in each count cycle (0-10+a-Z). instead of just 10 (0-10)

So the first membership number would still be 00001 where as the 12th would now be 0000a, 14th 0000c and 34th would be 0001a.

To summarize i need a way of defining the characters for counting in a way that can be generated based on the id of a user.

I hope I have explained this well enough.

  • 写回答

1条回答 默认 最新

  • dongyou26216708 2018-10-27 12:15
    关注

    Assuming that these are the only characters you want to use:

    0123456789abcdefghjkmnpqrstuvwxyz
    

    You can use base_convert() and strtr() to translate specific characters of the result to the characters you want.

    function mybase33($number) {
        return str_pad(strtr(base_convert($number, 10, 33), [
            'i' => 'j',
            'j' => 'k',
            'k' => 'm',
            'l' => 'n',
            'm' => 'p',
            'n' => 'q',
            'o' => 'r',
            'p' => 's',
            'q' => 't',
            'r' => 'u',
            's' => 'v',
            't' => 'w',
            'u' => 'x',
            'v' => 'y',
            'w' => 'z',
        ]), 5, '0', STR_PAD_LEFT);
    }
    
    echo "9 is ".mybase33(9)."
    ";
    echo "10 is ".mybase33(10)."
    ";
    echo "12 is ".mybase33(12)."
    ";
    echo "14 is ".mybase33(14)."
    ";
    echo "32 is ".mybase33(32)."
    ";
    echo "33 is ".mybase33(33)."
    ";
    echo "34 is ".mybase33(34)."
    ";
    

    Output:

    9 is 00009
    10 is 0000a
    12 is 0000c
    14 is 0000e
    32 is 0000z
    33 is 00010
    34 is 00011
    

    https://3v4l.org/8YtaR

    Explanation

    The output of base_convert() uses these characters:

    0123456789abcdefghijklmnopqrstuvw
    

    The strtr() translates specific characters of that output to:

    0123456789abcdefghjkmnpqrstuvwxyz
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测