str_shuffle 


string apg

RANDOMLY shuffles a string.





This function shuffles a STRING, and one permutation of all possible is created.

This function does not generate cryptographically secure values, and should not be used for cryptographic purposes.

There are other functions specially designed for this purpose.

Since the PHP 7.1.0 internal randomization algorithm has been changed to use the Mersenne Twister Random Number Generator instead of the libc rand function.

Natural Order String Comparison  


This function has problems with some characters.



<?php

str str_shuffle 
str $str )


where,

$str The STRING to be shuffled

?>
 

$str


The input STRING.



  1 EXERCISE   

<?php

$str01 
"There's no smoke without fire!'";

/*
  Try also:
  
  $str01 = 'There\'s no smoke without fire!';
*/

$shuffld01 str_shuffle($str01);

echo 
$shuffld01 '<br><br>';

?> 

  2 EXERCISE   

<?php

$str02 
'1234567890';

$shuffld02 str_shuffle($str02);

echo 
$shuffld02 '<br><br>';

echo 
'Shuffled value:<br>';
var_dump($shuffld02);
echo 
'<br><br>Given value:<br>';
var_dump($str02);

?> 

  3 EXERCISE   

<?php

/*
 * Testing str_shuffle() : basic functionality
*/

echo "Testing str_shuffle() : basic functionality.<br>";

// Initialize all required variables
$str 'This testcase tests the 
        str_shuffle() function.'
;
        
/*
// Try also:

$str = "This testcase tests the 
        str_shuffle() function.";
*/
                    
$strs str_shuffle($str);
                    
echo 
"<br>Given string:<br>'$str'<br><br>
Shuffled string:<br>'
$strs'<br><br>";


// For a given i/p string ensure that 
// all combinations are
// generated given a reasonable 
// sample of calls

$a = array();
$trys 1000;
$ip 'abcd';
$len_ip strlen($ip);

for (
$i 0$i $trys$i++) {
    
$op str_shuffle($ip);

    if (!
is_string($op) || strlen($op) != $len_ip) {
        echo 
"TEST FAILED<br>";
    }

    
// Combination already hit ?
    
if (empty($a[$op])) {
        
// No first time init
        
$a[$op] = 0;
    }

    
// Increment count for this combination
    
$a[$op]++;
}

$combinations count($a);

if (
$combinations != 24) {
    echo 
"TEST FAILED.. 
    Only 
$combinations out of a possible 
    24 combinations used<br>"
;
} else {
    echo 
"TEST PASSED<br>";
}

?>

  4 EXERCISE   

<?php

$str04 
"123âáàAGHéñÑíÕôúÚ";

/*
  Try also:
  
  $str04 = '123âáàAGHéñÑíÕôúÚ';
*/

$shuffld04 str_shuffle($str04);

echo 
$shuffld04 '<br><br>';

?>