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.…

7 years ago

Current Site URL – Codeigniter

To get host url of current server simply replace application\config\config.php [crayon-692bd8339a576123553346/] with [crayon-692bd8339a57b700877029/]  

7 years ago

PHP: fopen error handling

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

7 years ago

PHP function to make slug (URL string)

Note: from WordPress Use it like this: [crayon-692bd8339a7a9615663235/] Code: [crayon-692bd8339a7ad107497760/]  

7 years ago

Include all files with in the folder

[crayon-692bd8339a8b8296953059/]  

7 years ago

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

[crayon-692bd8339aaa3558083930/] or [crayon-692bd8339aaa8779187935/]  

8 years ago