How to generate a small set of alphanumeric characters in PHP?

Asked

Viewed 191 times

-3

How to generate a small set of PHP characters like this M7kL8a8z7q?

I want to upload several documents and I want to rename the files in PHP as the image button does here in Stack Overflow. If I mix md5, with the date and others gets huge.

You don’t have to follow this order of characters, you just have to be random, not repeat and have 10 characters.

  • It could not be a current timespan or a datetime?

  • 1
  • See if this suits you: https://ideone.com/1bwioP

1 answer

0

A simple password generator, you can change the password size by changing the variable length, and possible characters by changing the variable characters. Source

function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}
  • I tidied up, got confused and ended up putting an answer with JAVA

Browser other questions tagged

You are not signed in. Login or sign up in order to post.