Generate a Random String

This function generates a random string of a specified length. It uses str_shuffle to randomize a string of all alphanumeric characters, then uses substr to get a part of it that’s the desired length. This function is useful for generating random identifiers or tokens.

PHP Function:

function generateRandomString($length = 10) { return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length); }

// Example
echo generateRandomString(5); // 'a3G7s'
echo generateRandomString(10); // '4kU91mBvHq'

Member since January 2, 2019

As a seasoned WordPress developer with expertise in various tech stacks and languages, I bring years of experience to every project I handle. My passion for coding and dedication to delivering exceptional work ensures that each project I take on is of the highest quality. I specialize in creating custom themes, developing plugins, and building full-scale web systems. By staying up-to-date with the latest industry trends and best practices, I incorporate cutting-edge solutions into my work.

Comments

    Your email address will not be published. Required fields are marked *