输出随机单词 [英] Output random words
问题描述
我必须写一个程序,将输出随机单词(随机字母),而且不会有3人声或接近辅音,所以我写了:
I have to write a program that will output random words(with random letters) and that will not have 3 vocals or consonants near, so i wrote:
$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>
不知道为什么,但看来,如果不工作,因为当它输出,它会同时打印的话在3以上的辅音或发声。
有谁能够帮助我?感谢:D和英语不好对不起:P
don't know why but it seems that IF doesn't work, 'because when it outputs it'll print also words with 3 or more consonants or vocal. Can anybody help me? thanks :D and sorry for bad english :P
推荐答案
这应该为你工作:
所以,你有什么我所做的一切?
So what have I done here?
在第一部分中我已经定义了其中包含的所有字符数组 $字符
例如 [A-ZA-Z]
。然后我也有 $元音
包含例如,全元音 [aAeEiIoOuU]
。而且我也定义 $辅音
包含所有辅音。
In the first part I have defined the array $characters
which contains all characters e.g.[a-zA-Z]
. Then I also have $vowels
which contains all vowels e.g. [aAeEiIoOuU]
. And I also defined $consonants
which contains all consonants.
这之后,我混整个 $字符
阵列的 洗牌()
并把 $长度
从一开始就与<一个数组元素HREF =http://php.net/manual/en/function.array-slice.php相对=nofollow> array_slice()
。
After this I mixed the entire $characters
array with shuffle()
and took $length
array elements from the start with array_slice()
.
然后我检查了 $ randomWord
通过功能检查()
,其中经过随机的每一个字符字,并检查是否在接下来的3个字符或者是元音或辅音。我这样做与 in_array()
检查字符是在关联数组如 $元音
或 $辅音
。如果是的话我更改行的中间人物到对面的如元音
- > 辅音
| 辅音
- > 元音
。如果没有行无论是元音辅音还是被发现则返回随机字。
Then I checked the $randomWord
with the function check()
, which goes through each character of the random word and checks if the next 3 characters are either vowels or consonants. I do this with in_array()
to check if the character is in the associated array e.g. $vowels
or $consonants
. If yes I change middle character of the row to the opposite e.g. vowels
-> consonants
| consonants
-> vowels
. And if no row either of vowels or consonants are found it returns the random word.
最后我只使用 array_map()
与 CHR()
要经过每个数组元素,并将其从ASCII更改为匹配的字符。
At the end I just use array_map()
with chr()
to go through each array element and change it from ASCII to the matching character.
要打印出来我用 破灭()
每个字符一起追加。
To print it I used implode()
to append each character together.
<?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
这篇关于输出随机单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!