How can I generate a (pseudo)random alpha-numeric string, something like: 'd79jd8c' in PHP?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
1 line:
Maybe I missed something here, but here's a way using the uniqid() function.
You can use the following code, copied from this article. It is similar to existing functions except that you can force special character count:
The modern way to do that with type hint / rand_int for real randomeness
If you want a very easy way to do this, you can lean on existing PHP functions. This is the code I use:
substr( sha1( time() ), 0, 15 )
time()
gives you the current time in seconds since epoch,sha1()
encrypts it to a string of 0-9a-f, andsubstr()
lets you choose a length. You don't have to start at character 0, and whatever the difference is between the two numbers will be the length of the string.