[英]How to shuffle the letters of a word in php
我在一些SO帖子中看到,要改组数组,可以使用php的shuffle($array);
。
但是,我的问题是我从字典中生成了一个随机词(我知道该怎么做并且正在工作),然后将字母随机地随机排列成一个字谜。
所以基本上adrmon
这个词会出现在adrmon
或类似的词上,但是它会random
adrmon
单词中的字母。
如何在php中做到这一点?
function shuffleWord($word) {
$wordArray = str_split($word);
shuffle($wordArray);
return implode('',$wordArray);
}
$word = 'random';
$anagram = shuffleWord($word);
您可以在PHP中使用str_shuffle函数。
PHP脚本:
<?php
//For shuffling characters in a string you can use str_shuffle function as shown below
echo str_shuffle("PHPTUTORS");
?>
输出:
TSTPPOHRU
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.