Jim
Jim

Reputation: 63

How can I randomize an entire string of 62 characters?

I have 62 base64 characters that I want to randomize. How can I do this using PHP? The string would be all letters, upper and lower case as well as numbers from 0-9.

The thing that is most important to me is that the entire string be evaluated before a return value is given. In other words, if I request a string of 8 characters in length and my string starts out like:

1234567890ABCDE..... I don't want to get the first 8 numbers randomized. It should randomize the entire string first, then return 8 characters from that.

Upvotes: 6

Views: 118

Answers (2)

Tatu Ulmanen
Tatu Ulmanen

Reputation: 124758

Try this:

$string = '1234567890ABCDE...';

$string = substr(str_shuffle($string), 0, 8);

str_shuffle randomizes the string, then substr takes the first 8 characters from it.

Upvotes: 6

Yacoby
Yacoby

Reputation: 55445

Take a look at str_shuffle.

Upvotes: 5

Related Questions