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

6 years ago

Current Site URL – Codeigniter

To get host url of current server simply replace application\config\config.php [crayon-679fd514c9b67138411821/] with [crayon-679fd514c9b77284462246/]  

6 years ago

PHP: fopen error handling

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

6 years ago

PHP function to make slug (URL string)

Note: from WordPress Use it like this: [crayon-679fd514c9d4a111587355/] Code: [crayon-679fd514c9d4e281470216/]  

6 years ago

Include all files with in the folder

[crayon-679fd514c9e2a639577072/]  

6 years ago

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

[crayon-679fd514c9eb6332957350/] or [crayon-679fd514c9eb9558249241/]  

7 years ago