【问题标题】:How to shuffle the letters of a word in php如何在php中打乱单词的字母
【发布时间】:2012-01-27 07:43:14
【问题描述】:

我在一些 SO 帖子上看到要随机排列数组,您可以使用 php 的 shuffle($array);

但是,我的问题是我从字典中生成了一个随机单词(我知道该怎么做并且正在工作),然后将这些字母随机打乱成一个字谜。

所以基本上 - random 这个词会转到 adrmon 或类似的东西,但它会随机打乱单词中的字母。

如何在 php 中做到这一点?

【问题讨论】:

    标签: php random shuffle words anagram


    【解决方案1】:
    function shuffleWord($word) {
    
        $wordArray = str_split($word);
        shuffle($wordArray);
        return implode('',$wordArray);
    }
    
    $word = 'random';
    $anagram = shuffleWord($word);
    

    【讨论】:

      【解决方案2】:

      From PHP manual - str_shuffle

      您可以在 PHP 中使用 str_shuffle 函数。

      【讨论】:

        【解决方案3】:

        PHP 脚本:

        <?php
        //For shuffling characters in a string you can use str_shuffle function as shown below
        echo str_shuffle("PHPTUTORS");
        ?>
        

        输出:

        TSTPPOHRU
        

        Reference Source Code

        【讨论】:

          猜你喜欢
          • 2017-07-10
          • 1970-01-01
          • 1970-01-01
          • 2018-08-22
          • 2015-10-28
          • 2014-04-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多