Categories: Snippets

Random String 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;
}

Output the random string with the call below:

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

 

Share

Recent Posts

Clear the browser cache of CSS or JavaScript Using PHP

Other than caching every hour, or every week, you may cache according to file data.…

5 years ago

Current Site URL – Codeigniter

To get host url of current server simply replace application\config\config.php [crayon-66342e0a02db6175732544/] with [crayon-66342e0a02dbc372556694/]  

5 years ago

PHP: fopen error handling

You should first test the existence of a file by file_exists(). [crayon-66342e0a02f02259412588/] or simple solution…

5 years ago

PHP function to make slug (URL string)

Note: from WordPress Use it like this: [crayon-66342e0a03013995730739/] Code: [crayon-66342e0a03019035874193/]  

5 years ago

Include all files with in the folder

[crayon-66342e0a03181655644838/]  

6 years ago

How can I remove 3 characters at the end of a string in php?

[crayon-66342e0a0326b220310199/] or [crayon-66342e0a03284251337977/]  

6 years ago