php word generator

Help me create a word generator in php. You need to generate words from the following letters:
e, r, t, u, i, o, p, a, s, d, f, g, h, k, l, z, v, b, n, m

The length of the resulting word is 5 characters.
From the generation you need to remove the words in which:

  • Contains 2 or more vowels in a row
  • Consecutive 2 or more identical consonants
  • Consecutive 3 or more consonants

All data must be written to a file.

That is:

eraab, eraaa, errab, ertde – such words are removed

ertab, ritar – these remain


Answer 1, authority 100%

$minlen = 5;
$maxlen = 10;
$num = 10;
$l = array('euioa', 'rtpsdfghklzvbnm');
$ll = array(strlen($l[0]), strlen($l[1]));
$out = array();
$ll0 = sizeof($l[0]);
$ll1 = sizeof($l[1]);
for($j = 0; $j < $num; ++$j){
    $cl = rand($minlen, $maxlen);
    $word = '';
    for($i = 0; $i < $cl; ++$i){
        if($n === 0) $n = 1;
        else $n = rand(0, 1);
        do{
            $s = $l[$n][rand(0, $ll[$n]-1)];
        }while($s == $word[strlen($word)-1]);
        $word .= $s;
    }
    $out[] = $word;
}
print_r($out);

A slightly more general solution, involving a variable length of nicknames. If you need exactly 5 letters, change $maxlen to 5.


Answer 2, authority 80%

There are two ways.
1) simple:
loop through all the words from aaaaa to zzzzz.

if(preg_match("/(?:[euioa]{2,})|(?:[qwrtypsdfghjklzxcvbnm]{3,})/",$word){
//  ,   1  3
}
$doubles = str_split('qqwwrrttyyppssddffgghhjjkkllzzxxccvvbbnnmm',2);
if(strpos($word,$doubles)!==false){
// -  
}

Answer 3, authority 60%

Brute force:

const VOWS = 0;
const CONS = 1;
const ALL = 2;
$lists = array(
    VOWS => array(),
    CONS => array(),
    ALL => array()
);
foreach (array('euioa', 'rtpsdfghklzvbnm') as $i => $chars) {
    for ($j = 0; $j < strlen($chars); $j++) {
        $char = $chars[$j];
        $lists[$i][$char] = $i + 1;
        $lists[ALL][$char] = 1 - $i;
    }
}
function generate($chars, $word = '', $level = 0) {
    global $lists;
    if ($level == 5) {
        echo "$word\n";
        return;
    }
    foreach ($chars as $char => $type) {
        $list = $lists[$type];
        unset($list[$char]);
        generate($list, $word.$char, $level + 1);
    }
}
generate($lists[VOWS] + $lists[CONS]);

Answer 4, authority 20%

And what exactly is your problem?

Normal depth-first search with limited depth. As well as checking the rules at each generation step. There will be many options, I immediately warn you.

As for generating nicknames, I would recommend a genetic algorithm. Before that, having taught him some kind of list of beautiful nicknames. Then it will generate no worse 🙂