doutan5337 2011-04-14 06:22
浏览 53

Perl - PHP:随机字符串生成

I don't know perl language, but i am trying to customize an application written in perl language. I want to know the logic or meaning of the following perl code. I know this code is for generating a random string, but i want its details i.e i want to know how can i generate this same random string in PHP? Please anyone give me the PHP code for this perl code!!

   sub generate_rand_string {

    #warn "generate_rand_string";

    my $chars = shift
      || 'aAeEiIoOuUyYabcdefghijkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789';
    my $num = shift || 1024;

    require Digest::MD5;

    my @chars = split '', $chars;
    my $ran;
    for ( 1 .. $num ) {
        $ran .= $chars[ rand @chars ];
    }
    return Digest::MD5::md5_hex($ran);
}

Please anyone help me!!!!

  • 写回答

2条回答 默认 最新

  • drsh30452 2011-04-14 06:34
    关注

    In PHP it would like this:

    function generate_rand_string($chars = null, $length = 1024) {
        if($chars == null) {
            $chars = 'aAeEiIoOuUyYabcdefghijkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789';
        }
    
        $rand = '';
        for($i = 0; $i < $length; $i++) {
            $rand .= $chars[ rand(0, strlen($chars) - 1) ];
        }
    
        return md5($rand);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?