I know that the rand function in PHP generates random integers, but what is the best way to generate a random string such as:
Original string, 9 chars
$string = 'abcdefghi';
Example random string limiting to 6 chars
$string = 'ibfeca';
UPDATE: I have found tons of these types of functions, basically I'm trying to understand the logic behind each step.
UPDATE: The function should generate any amount of chars as required.
Please comment the parts if you reply.
Well, you didn't clarify all the questions I asked in my comment, but I'll assume that you want a function that can take a string of "possible" characters and a length of string to return. Commented thoroughly as requested, using more variables than I would normally, for clarity:
To call this function with your example data, you'd call it something like:
Note that this function doesn't check for uniqueness in the valid chars passed to it. For example, if you called it with a valid chars string of
'AAAB'
, it would be three times more likely to choose an A for each letter as a B. That could be considered a bug or a feature, depending on your needs.