Generating random strings with custom specified length is most required functionality in every project. In this article, I described the easiest way to generate custom length random string.
PHP program for Random String Generate:
<?php
/*
* Function to generate random string
* Default length 30 character
*/
function generateRandomString($length = 30)
{
// Control random character by manual
$wordArray = explode(",", "a, b, c, d, e, f, g, h, i, j, k, l, m, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0");
//Shuffle array values
shuffle($wordArray);
//Merge shuffled array values into string and fetch first X ($length) characters
return substr(implode($wordArray, ""), 0, $length);
}
echo "Random string with length 30: ".generateRandomString();
echo "Random string with length 10: ".generateRandomString(10);
echo "Random string with length 8: ".generateRandomString(8);
?>
Creating an array with the name ‘$wordArray‘. Store custom required random string characters.
// Control random character by manual
$wordArray = explode(",", "a, b, c, d, e, f, g, h, i, j, k, l, m, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0");
Now, shuffle the random string character array with the default PHP function ‘shuffle()‘. It randomizes the order of the elements in an array.
//Shuffle array values
shuffle($wordArray);
After shuffled, merge the given array value into a string by using default PHP function ‘implode‘ and split / fetch $length character from the first position.
//Merge shuffled array values into string and fetch first X ($length) characters
return substr(implode($wordArray, ""), 0, $length);
Note: You can also split the string with custom position.
//Merge shuffled array values into string and fetch first X ($length) characters
return substr(implode($wordArray, ""), 5, $length); # It will fetch from 5 th position
Output:
Random string with length 30: m1ehb9f36d0j2k854ai7gcl
Random string with length 10: 04gjbhlm9a
Random string with length 8: 8b49ah0f

Related Posts
-
PHP: Convert JSON String to Array / Object using json_decodeHow to convert JSON String to Array / Object using json_decode? json_decode is inbuilt function in PHP. It will take a JSON string and converts it into a PHP array or object depends on arguments. Live demo: https://www.gtechhub.com/json-decode-online Convert JSON String to PHP Object: <?php $json_input = '{"array_key_1":"array_value_1","array_key_2":"array_value_2", "array_key_3":"array_value_3","array_key_4":"array_value_4","array_key_5":"array_value_5"}'; $json_result…
-
PHP: Create JSON String from Array / Object - json_encode()json_encode() function is an inbuilt PHP function which is used to convert PHP array or object into JSON representation. Encoding is used to bundle data with respect to a particular format. This process will be required to preserve data consistency. Convert array to JSON: <?php $input_ary = array(); $input_ary['array_key_1'] = 'array_value_1'; $input_ary['array_key_2']…
How to add and subtracting days using php date functionPHP date() function formats a local date and time, and returns the formatted date string. In this article, I described the simple process about how to add and subtract number of days from current date using default PHP date() function. Add number of days in current date: php strtotime add…
-
PHP: Create empty object - new stdClass,var_dumpstdClass is the default PHP object. stdClass has no properties, methods or parent. It does not support magic methods, and implements no interfaces. Standard way to create an "empty" object is: <?php $input_object = new stdClass(); var_dump($input_object); ?> Result for new stdClass(): object(stdClass)#1 (0) { } But, with PHP Version…
How to add and subtracting weeks using php date() functionPHP date() function formats a local date and time, and returns the formatted date string. In this article, I described the simple process about how to add and subtract number of weeks from current date using default PHP date() function. Add number of weeks in current date: (Example: adding 5 weeks) <?php…
Get the last element of an array in PHP – end()The end() function is an inbuilt function in PHP Programming. The purpose of this function is to find the last element of the given input array. The end() function changes the internal pointer of an array to the last element and returns the value of the last element. end() PHP…
-
PHP: How to copy all files from one directory to anotherI will guide you to copy all files & sub directories of the directory to another directory using PHP copy() function. There are many other functions used to copy the content of one directory to another. copy() Functionopendir() Functionis_dir() Functionscandir( ) Functionreaddir() Function PHP code for copy multiple files from…
PHP Cheat SheetPHP code is usually processed on a web server by a PHP interpreter implemented as a module, a daemon or as a Common Gateway Interface (CGI) executable. On a web server, the result of the interpreted and executed PHP code — which may be any type of data, such as…
How to add and subtracting months using php date() functionPHP date() function formats a local date & time, it returns the formatted string formatted date. In this article, I described the simple process about how to add and subtract number of months from the current date using default PHP date() function. Add number of months in current date: (Example:…
-
How to get / list out installed PHP extension in Linux OSLinux widely uses operating system in the server side and client side. Operating system provides flexible options to install our custom packages and extensions based on our project requirement. Additionally, we can write our own shell scripts to automate our projects. PHP is used mainly in server-side application software along with various…