Hi i want to check with preg_match_all
if a string consist of vowel and consonant.
The structure should be as consonant|vowel|consonant|vowel
.
For example:
xaxa
xuxu
baba
nunu
Hi i want to check with preg_match_all
if a string consist of vowel and consonant.
The structure should be as consonant|vowel|consonant|vowel
.
For example:
xaxa
xuxu
baba
nunu
This is a bit tricky because, while my first instinct was to match vowels and then not-vowels, It also matched everything else. Then there are rules in English with y
sometimes being a vowel.
This should get you there.
$string = "xaxa xuxu baba nunu nnnn";
$vowels = 'aeiouy';
$consonants = 'bcdfghjklmnpqrstvwxyz';
$pattern = "!([$consonants][$vowels][$consonants][$vowels])!i";
$found = preg_match_all($pattern,$string,$matches);
foreach ($matches[0] as $value){
echo $value."<br />";
}
Strange thing about English is this will also match yyyy
. If you wish not to treat y
as a vowel, you can remove it from the group.
$vowels = 'aeiou';