微麦PHP 2019-06-26
string str_shuffle ( string $str ) //str_shuffle() shuffles a string. One permutation of all possible is created. //str_shuffle() 函数打乱一个字符串,使用任何一种可能的排序方案。
Caution
<?php
/**
* Created by PhpStorm.
* User: zhangrongxiang
* Date: 2018/3/7
* Time: 下午10:04
*/
$str = "abcde";
echo str_shuffle( $str ) . PHP_EOL;
echo str_shuffle( $str ) . PHP_EOL;
echo str_shuffle( $str ) . PHP_EOL;
echo str_shuffle( $str ) . PHP_EOL;
/////////////////////////////////////////////////////////////////////////////////
function scramble_word( $word ) {
if ( strlen( $word ) < 2 ) {
return $word;
} else {
return $word{0} . str_shuffle( substr( $word, 1, - 1 ) ) . $word{strlen( $word ) - 1};
}
}
//echo preg_replace('/(\w+)/e', 'scramble_word("\1")', 'A quick brown fox jumped over the lazy dog.');
echo scramble_word( "A quick brown fox jumped over the lazy dog." ) . PHP_EOL;
///////////////////////////////////////////////////////////////////////////////
/**This function is affected by srand():*/
srand( 12345 );
echo str_shuffle( 'Randomize me' ) . PHP_EOL; // demmiezR aon
echo str_shuffle( 'Randomize me' ) . PHP_EOL; //izadmeo Rmen
srand( 12345 );
echo str_shuffle( 'Randomize me' ) . PHP_EOL; // demmiezR aon
////////////////////////////////////////////////////////////////////////////
// not very true
srand(time());
function unicode_shuffle( $string, $chars, $format = 'UTF-8' ) {
$rands = [];
for ( $i = 0; $i < $chars; $i ++ ) {
$rands[ $i ] = rand( 0, mb_strlen( $string, $format ) );
}
$s = null;
foreach ( $rands as $r ) {
$s .= mb_substr( $string, $r, 1, $format );
}
return $s;
}
echo unicode_shuffle( "万物结对象", 3 ) . PHP_EOL;