PHP 7 Script to Generate Random String by Shuffling String Using str_shuffle() Method Full Project For Beginners

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments

 

Welcome folks today in this blog post we will be generating random string by shufling string using str_shuffle() method using php 7. All the full source code of the application is shown below.

 

 

 

Get Started

 

 

 

In order to get started you need to make an index.php file and copy paste the following code

 

 

In the first example we will be generating random strings of fixed length

 

 

index.php

 

 

<?php
$str = 'abcdefgh';//range for string generation
$shufflestr = str_shuffle($str);//used to shuffle the range of strings
echo $shufflestr;
?>

 

 

Full Example

 

 

index.php

 

 

<?php 

function RandomString($num) 
{ 
  // Variable that store final string 
  $final_string = ""; 
  
  //Range of values used for generating string
  $range = "_!@#$^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 
  
  // Find the length of created string 
  $length = strlen($range); 
  
  // Loop to create random string 
  for ($i = 0; $i < $num; $i++) 
  { 
    // Generate a random index to pick 
    // characters 
    $index = rand(0, $length - 1); 
    
    // Concatenating the character 
    // in resultant string 
    $final_string.=$range[$index]; 
  } 
  
  // Return the random generated string 
  return $final_string; 
} 

//Creating call to test above function 
$num = 5; 
echo "Random String of length " . $num
. " = " . RandomString($num); 
?>

Leave a Reply