note 100159 deleted from function.str-shuffle by danbrown
| From: | danbrown@php.net | Date: | Tue, 28 Sep 2010 21:06:12 +0000 |
| Subject: | note 100159 deleted from function.str-shuffle by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172787@lists.php.net to get a copy of this message | ||
Note Submitter: ozkankonay at gmail dot com
----
/**
* @param string $str #desc: text to be mixed.
* @param bool $unicode #desc: multilanguage(utf8) support.
* @param bool $force #desc: if is true, all the texts longer than two chars will be forced to
mix; otherwise they sometimes can be same.
* @return string
*/
function str_mixer($str, $unicode=true, $force=false){
if( empty($str) ) return $str;
if( !$unicode ) return str_shuffle($str);
$chars = array();
if( preg_match_all('/[\s\S]/u', $str, $matches) ) {
if( !isset($matches[0]) || !is_array($matches[0]) ) return $str;
$chars = $matches[0];
}
if( count($chars) == 0 ) return $str;
$tmpchars = $chars;
shuffle($chars);
/**
* # manual shuffle #
*
* srand((double)microtime()*1000000);
* $length = count($chars);
* for($i = 0; $i < $length; $i++){
* $rand = rand(0, $length - 1);
* $tmp = $chars[$i];
* $chars[$i] = $chars[$rand];
* $chars[$rand] = $tmp;
* }
*/
if( $force ){
if( strlen($str) > 2 && $tmpchars === $chars ) return str_mixer($str);
}
return implode('', $chars);
}
/**
* str_mixer() test :
*/
$testvar = 'özkan';
echo str_mixer($testvar); // output : aöknz
echo str_mixer($testvar, false); // output : nöazkn�kza� -> unexpected
result
echo str_mixer($testvar, true, true); // output : öankz