dongshicuo4844 2017-08-14 02:59
浏览 91

如何使用交替的字母和数字生成随机的6个字符的字符串?

I have a code in PHP that is able to generate a 6-character alpha-numeric string but it does not ensure that there are 3 letters and 3 numbers generated.

It generates a string such as "ps7ycn" It does not have 3 numbers in between the alphabet. The numbers should be in between the letters.

example : a3g5h9

  • 写回答

4条回答 默认 最新

  • doutuan4361 2017-08-14 03:50
    关注

    Here's one way you could address this:

    $alpha  = 'abcdefghijklmnopqrstuvwxyz';
    $number = '0123456789';
    
    $random = '';
    
    for ( $i = 0; $i < 6; ++$i ) {
        if ( $i % 2 ) {
            $random .= substr( $number, rand( 0, strlen( $number ) - 1 ), 1 );
        } else {
            $random .= substr( $alpha, rand( 0, strlen( $alpha ) - 1 ), 1 );
        }
    }
    

    $random will now contain a six-character random value with the first, third, and fifth characters coming from $alpha and second, fourth, and sixth characters coming from $number.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作