Open In App

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

Last Updated : 21 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:

PHP




<?php
     
// Function to generate password from 
// given string
function get_password($str, $len = 0) {
      
    // Variable $pass to store password
    $pass = "";
      
    // Variable $str_length to store 
    // length of the given string
    $str_length = strlen($str);
   
    // Check if the $len is not provided
    // or $len is greater than $str_length
    // then assign $str_length to $len
    if($len == 0 || $len > $str_length){
        $len = $str_length;
    }
   
    // Iterate $len times so that the 
    // resulting string's length is 
    // equal to $len
    for($i = 0;  $i < $len; $i++){
          
        // Concatenate random character 
        // from $str with $pass
        $pass .=  $str[rand(0, $str_length)];
    }
    return $pass;
}
   
   
// Print password of length 5 from 
// the given string
$str = "GeeksForGeeks";
echo get_password($str, 5) . "\n<br/>";
   
// Print password of length 15 from
// the given string
$str
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
echo get_password($str, 15) ."\n<br/>";
   
// Print password if the length 
// is not given
echo get_password($str) . "\n<br/>";
   
?>


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:

PHP




<?php
     
// Function to generate password from
// given string
function get_password($str, $len = 0) {
      
    // Variable $pass to store password
    $pass = "";
     
    // Variable $str_length to store
    // length of the given string
    $str_length = strlen($str);
   
    // Check if the $len is not provided
    // or $len is greater than $str_length
    // then assign $str_length into $len
    if($len == 0 || $len > $str_length){
        $len = $str_length;
    }
   
    // Shuffle the string 
    $pass = str_shuffle($str);
         
    // Extract the part of string
    $pass = substr($pass, 0, $len);
     
    return $pass;
}
   
// Print password of length 5 from
// the given string
$str = "GeeksForGeeks";
echo get_password($str) . "\n<br/>";
   
// Print password of length 15 from
// the given string
$str
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
echo get_password($str, 15) ."\n<br/>";
   
// Print password if the length is
// not given
echo get_password($str) . "\n<br/>";
   
?>


Output:

oeFssGkeGrkee
rxIhAjCi47wgW1z
r4LZQqlOFGU36i7gEAtzwsnduNXhHKD92ajpxBJc1MRvVmbyeokPIWfCTSY85


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads