str_shuffle()函数是PHP中的内置函数,用于随机混洗作为参数传递给该函数的字符串的所有字符。传递数字时,它将数字视为字符串并Shuffle[洗牌]。此函数不会更改原始字符串或作为参数传递给它的数字。而是返回一个新字符串,该字符串是参数中传递给它的字符串的可能排列之一。
用法:
str_shuffle($string)
参数:该函数接受单个参数$string。参数$string指定需要改组其字符的字符串。代替字符串,也可以传递数字。如果传递数字而不是字符串作为参数,则此函数会将那个数字视为字符串。
Return Value:该函数返回一个相同长度的字符串,但其内部带有随机字符。每次执行程序时,都会显示不同的输出,因为每次字符改组都不同。在某些情况下,原始字符串或数字可能是返回值。
例子:
Input : $string = "raj" Output : jar Input : $string = "geeks" Output : eeksg Input : $string = 142 Output : 412 Note: The output will be different on every execution.
以下示例程序旨在说明str_shuffle()函数:
程序1:传递字符串时演示str_shuffle()函数的程序。
<?php
// PHP program to demonstrate the str_shuffle()
// fucntion when a string is passed
$string = "geeks";
// prints the shuffled string
echo str_shuffle($string);
?>
输出:
keegs
程序2:传递数字时演示str_shuffle()函数的程序。
<?php
// PHP program to demonstrate the str_shuffle()
// fucntion when a number is passed
$string = 132;
// prints the shuffled string
echo str_shuffle($string);
?>
输出:123
/** * 获取唯一随机字符串 * @param int $len * @return string */ function unique_random($len = 10) { $str = 'qwertyuiopasdfghjklzxcvbnmasdfgh'; str_shuffle($str); return substr(str_shuffle($str), 0, $len); }