【问题标题】:Output random words输出随机词
【发布时间】:2015-02-24 09:06:06
【问题描述】:

我必须编写一个程序来输出随机单词(带有随机字母)并且附近不会有 3 个人声或辅音,所以我写道:

$array = array();                                 
$n = 10;                                               

for($i = 0; $i < $n; $i++) {
    $l = rand(4, 10);  //$l = word length
?>

<br>

<?

for ($j = 0; $j < $l; $j++) {

    $cas = rand(65, 90);  //$cas=random letters
    $array[$j] = $cas; 

    if($j > 1) {    
        if (($array[$j-1] == 65 || $array[$j-1] == 69 || $array[$j-1] == 73 || $array[$j-1] == 79 || $array[$j-1] == 85) ^ ($array[$j-2] == 65|| $array[$j-2] == 69 || $array[$j-2] == 73 || $array[$j-2] == 79 || $array[$j-2] == 85)) {  //will do XOR '^'

            $cas = rand(65, 90);                                                           
            $array[$j] = $cas;                          

        }

    }

    $m = chr($array[$j]);   
    echo $m;
    }
}


?>

</body>
</html>

不知道为什么,但似乎 IF 不起作用,因为当它输出时,它还会打印带有 3 个或更多辅音或声乐的单词。 有谁能够帮助我?谢谢:D,抱歉英语不好:P

【问题讨论】:

    标签: php html arrays random words


    【解决方案1】:

    这应该适合你:

    那么我在这里做了什么?

    在第一部分中,我定义了数组$characters,其中包含所有字符,例如[a-zA-Z]。然后我还有$vowels,其中包含所有元音,例如[aAeEiIoOuU]。我还定义了包含所有辅音的$consonants

    在此之后,我将整个$characters 数组与shuffle() 混合,并从array_slice() 开始获取$length 数组元素。

    然后我用函数check()检查了$randomWord,它遍历随机单词的每个字符并检查接下来的3个字符是元音还是辅音。我用in_array() 来检查字符是否在关联的数组中,例如$vowels$consonants。如果是,我将行的中间字符更改为相反,例如vowels -> consonants | consonants -> vowels。如果没有找到任何元音或辅音行,则返回随机单词。

    最后我只是使用array_map()chr() 来遍历每个数组元素并将其从ASCII 更改为匹配字符。

    为了打印它,我使用implode() 将每个字符附加在一起。

    <?php
    
        $characters = array_merge(range(65, 90), range(97, 122));
        $vowels = array(65, 97, 69, 101, 73, 105, 79, 111, 85, 117);
        $consonants  = array(66, 98, 67, 99, 68, 100, 70, 102, 71, 103, 72, 104, 74, 106, 75, 107, 76, 108, 77, 109, 78, 110, 80, 112, 81, 113, 82, 114, 83, 115, 84, 116, 86, 118, 87, 119, 88, 120, 89, 121, 90, 122);
        $randomWord = "";
        $length = rand(4, 10);
    
        shuffle($characters);
        $randomWord = array_slice($characters, 0, $length);
    
        function check($randomWord, $vowels, $consonants) {
    
            foreach($randomWord as $key => $randomCharacter) {
    
                //Check for vowels
                if(in_array($randomWord[$key], $vowels) && ( isset($randomWord[$key+1]) && in_array($randomWord[$key+1], $vowels) ) && ( isset($randomWord[$key+2]) && in_array($randomWord[$key+2], $vowels) )) {
                    $randomWord[$key+1] = $consonants[array_rand($consonants, 1)];
                    check($randomWord, $vowels, $consonants);   
                } 
    
                //Check for consonants
                if(in_array($randomWord[$key], $consonants) && ( isset($randomWord[$key+1]) && in_array($randomWord[$key+1], $consonants) ) && ( isset($randomWord[$key+2]) && in_array($randomWord[$key+2], $consonants) )) {
                    $randomWord[$key+1] = $vowels[array_rand($vowels, 1)];
                    check($randomWord, $vowels, $consonants);
                } 
    
            }
    
            return $randomWord;     
        }
    
        $randomWord = check($randomWord, $vowels, $consonants);
        echo $randomWord = implode("", array_map("chr", $randomWord));
    
    ?>
    

    示例输出:

    YeVIkufuhv
    lEMObi
    VosaKAzIRb
    qOyoK
    IBoVIQahIg
    

    【讨论】:

    • 感谢您的回答!我认为这是个好主意,尽管我无法更改我已经写的内容,所以我必须按照代码提出问题!还是谢谢!
    • @Mattia 为什么你不能改变?
    • 有人告诉我要那样做这个程序 :)
    • @Mattia 我认为你的尝试不是很快,也不是很容易阅读
    • 我知道人,但在第一部分我随机选择长度和字符,然后我必须检查这个词(不是 3 个辅音等).. 就是这样!我只需要检查,IF 或 WHILE,以及如何去做!
    【解决方案2】:

    此代码对我来说是私有的,您可以使用它

    function random_string($hashstring=null,$randLengh=null)
        {
            $string = $hashstring;
            $randLengh =$randLengh;
            if ($string == null) {
                $string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            }
            $charactersLength = strlen($string);
            $randomString = '';
            for ($i = 0; $i < $randLengh; $i++) {
                $randomString .= $string[rand(0, $charactersLength - 1)];
            }
    
            return $randomString;
        }
    

    【讨论】:

    • 不幸的是我忘了说我必须使用ASCII码所以我不能定义一个包含所有字符的变量!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2023-01-27
    • 2011-06-16
    • 2021-11-29
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多