how to generate a random string + text + multiple

2019-09-23 09:58发布

问题:

I actually got this script from here... but it is not quite what I needed...

<?php
function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

// Echo the random string.
// Optionally, you can give it a desired string length.
<?php echo generateRandomString(); ?>
?>

basically, I wanted this small php script to generate thousands of lines (~3000)...

in this format:

mytext/(the_random_string_here)
mytext/(the_random_string_here)
mytext/(the_random_string_here)
mytext/(the_random_string_here)
mytext/(the_random_string_here)

up to the very end (~3000 lines) with each line have a random string...

回答1:

you can use a for loop to generate it 3000 times

for($i = 0; $i < 3000; $i++){
    echo 'mytext/'.generateRandomString().'<br>';
}

The answer was inside your code snippet that you provided



回答2:

If the number of lines is known, a for loop is a good choice. There is a nice example of such a loop right there in the generateRandomString function.

You could do something like this:

for ($i = 0; $i < 3000; $i++) {
   echo "mytext/(" . generateRandomString() . ")<br>\n";
}