Generating random text with PHP

You can generate random passwords, texts, etc. in any length with this code.

function generate_random_text($length = 3) {
    // Character list will be used in generating text
    $characters = 'abcdefghijklmnoprstuvyzxqwABCDEFGHIJKLMNOPRSTUVYZXQW0123456789';
    // split character string to character array
    $character_array = str_split($characters);
    $text = '';
    // choose random character from $character_array and generate string
    for ($i=0; $i < $length; $i++) {
        $text .= $character_array[mt_rand(0, strlen($characters) - 1)];
    return $text;
echo generate_random_text(5); // j4SaQ

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.