note 100158 added to function.str-shuffle
| From: | ozkankonayatgmaildotcom at osu1 dot php dot net | Date: | Tue, 28 Sep 2010 20:58:03 +0000 |
| Subject: | note 100158 added to function.str-shuffle | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172784@lists.php.net to get a copy of this message | ||
/**
* @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
----
Server IP: 160.75.100.7
Probable Submitter: 78.161.233.27
----
Manual Page -- http://www.php.net/manual/en/function.str-shuffle.php
Edit -- https://master.php.net/note/edit/100158
Del: integrated -- https://master.php.net/note/delete/100158/integrated
Del: useless -- https://master.php.net/note/delete/100158/useless
Del: bad code -- https://master.php.net/note/delete/100158/bad+code
Del: spam -- https://master.php.net/note/delete/100158/spam
Del: non-english -- https://master.php.net/note/delete/100158/non-english
Del: in docs -- https://master.php.net/note/delete/100158/in+docs
Del: other reasons-- https://master.php.net/note/delete/100158
Reject -- https://master.php.net/note/reject/100158
Search -- https://master.php.net/manage/user-notes.php