VOOZH about

URL: https://www.geeksforgeeks.org/php/how-to-generate-simple-random-password-from-a-given-string-using-php/

⇱ How to generate simple random password from a given string using PHP ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to generate simple random password from a given string using PHP ?

Last Updated : 23 Jul, 2025

In this article, we will see how to generate a random password using the given string. We have given a string and the task is to generate a random password from it.

Example:

Input: abgADKL123
Output: abgADKL123

Input: geeksforgeeks
Output: egksegsfroeke

To achieve this, we use the following approaches.

Approach 1: In this approach, we use the PHP rand() function and create a temporary variable, and using for loop we create random characters from the given string and concatenate that character to the temporary variable. Following is the implementation of this approach.

PHP code:

Output:

skGse
iWwf9jWZZE9ZL7O
GhRQ8zK4wpX93srUj1LhjsBEeBwBwo4Bh43RyZeSFZbwjVoonkKBgfXiBrEpY

Approach 2: In this approach, we use the PHP str_shuffle() function, and then we use the substr() function to extract the part of the given string.

PHP code:

Output:

oeFssGkeGrkee
rxIhAjCi47wgW1z
r4LZQqlOFGU36i7gEAtzwsnduNXhHKD92ajpxBJc1MRvVmbyeokPIWfCTSY85
Comment