Reputation: 161
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
Upvotes: 0
Views: 254
Reputation: 89547
This script check the alternating vowel/consonant for each word and returns them:
$subject = <<<LOD
abacagopa
titot
blux
apocop
pipo
laek
LOD;
$pattern = '~
# definitions
(?(DEFINE)(?<vowel>[aeiou]))
(?(DEFINE)(?<consonant>[bcdfghjklmnpqrstvwyz]))
# pattern
\b\g<vowel>?+(?>\g<consonant>\g<vowel>)*+\g<consonant>?+\b
~ix';
echo preg_replace($pattern, '$0<br/>', $subject);
Upvotes: 0
Reputation: 7870
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';
Upvotes: 1