按顺序生成带有预定义字符的 5 位字母数字代码 [英] Generating 5 digit alphanumeric code with predifined characters in sequence

查看:26
本文介绍了按顺序生成带有预定义字符的 5 位字母数字代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个由字母数字字符组成的会员编号,删除 iol 以防止打字时混淆.要在 php 中完成(如果重要的话也使用 Laravel 5.7 - 但我觉得这是一个 php 问题)

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)

如果简单地使用 0-9,则第一个会员编号将从 00001 开始,第 11 个人的会员编号为 00011.我想使用 0-9 + a-z 中的字母数字字符(删除所述字母)

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(共 10 个字符),abcdefghjkmnpqrstuvwxyz(共 23 个字符)——这在每个计数周期(0-10+a-Z)中总共给出 33 个字符.而不是 10 (0-10)

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)

所以第一个会员编号仍然是 00001,而第 12 个现在是 0000a,第 14 个是 0000c,第 34 个是 0001a.

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

总而言之,我需要一种定义用于计数的字符的方法,这种方法可以根据用户的 id 生成.

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.

推荐答案

假设您只想使用这些字符:

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

0123456789abcdefghjkmnpqrstuvwxyz

您可以使用 base_convert()strtr() 将结果的特定字符转换为您想要的字符.

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)."\n";
echo "10 is ".mybase33(10)."\n";
echo "12 is ".mybase33(12)."\n";
echo "14 is ".mybase33(14)."\n";
echo "32 is ".mybase33(32)."\n";
echo "33 is ".mybase33(33)."\n";
echo "34 is ".mybase33(34)."\n";

输出:

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

说明

base_convert() 的输出使用这些字符:

The output of base_convert() uses these characters:

0123456789abcdefghijklmnopqrstuvw

strtr() 将该输出的特定字符转换为:

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

0123456789abcdefghjkmnpqrstuvwxyz

这篇关于按顺序生成带有预定义字符的 5 位字母数字代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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