dszsajhd237437 2014-07-31 09:17
浏览 8
已采纳

生成密码并指定多少个degints和单词[关闭]

can somone help me to make this work i want to generate a password

Password Length: 15

Include Lowercase Characters Include Uppercase Characters

with one number in random position

ex:

Zs8ChnduGpkeuqx
ovephmVXD8RgcPr
VimDDE3txrVjLSe

here is what i was doing before and i wasnt sure if its the right way so i asked for help to get more informations ;)

<?php

function generateRandomString($length =14, $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'){
    $s = '';
    $lettersLength = strlen($letters)-1;

    for($i = 0 ; $i < $length ; $i++)
    {
        $s .= $letters[rand(0,$lettersLength)];
    }

    return $s;
}

 function generateRandomNum($length =1, $letters = '0123456789'){
    $s = '';
    $lettersLength = strlen($letters)-1;

    for($i = 0 ; $i < $length ; $i++)
    {
        $s .= $letters[rand(0,$lettersLength)];
    }

    return $s;
}

function shuffled() {
$str = generateRandomString();
$num = generateRandomNum();
$tot = $str.$num;  
$s = str_shuffle($tot);

{

    return $s;
}
}

echo shuffled();
?>

thank you for your help

  • 写回答

3条回答 默认 最新

  • du060334 2014-07-31 10:12
    关注

    Guessing you might benefit from an answer that walked you through what was going on, rather than just giving you wanted, might be useful for you in the future.

    function randomString($length) { // Generates a random string of $length characters long
        $letters = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ"; // Characters you don't mind having more than one of
        $random_string = '';
        for ($i = 0; $i < $length; $i++) { // Until $i = $length, do the following & increment $i by 1 each time
            $random_string .= $letters[rand(0, (strlen($letters) - 1))]; // Add another random character to the string
        }
        $random_string[rand(0, ($length - 1))] = rand(0,9); // Replace one of the characters with a random number between 0 and 9
    
        return $random_string;
    }
    
    echo randomString(15);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度