I want to check if a user is entering something close to a valid name. So if it's entered something like gbcvcvrt
, I would want to validate it and mark this as not a real name. Something like theg+vowel
should be validated as the right name.
On any length of string the code should detect four consecutive non vowel alphabets. I don't expect anyone's name to be rhythm by the way
Consider
$String = 'therhythm';
We can't check for the first four chars, we have to go through the entire string and check four any four consecutive non-vowel alphabets regardless of where on the string they are situated. That way we can validate $String as not valid even though the first five chars would validate it if we were checking the first four alphabets.
Based on your latest edit: You can use following regex for this:
^(?!.*?[^aeiou]{5})(?!.*?[aeiou]{3})[a-z]*$
Breaking it down:
[^aeiou]
=> Match anything but vowels(?!.*?[^aeiou]{5})
=> Negative lookahead to make sure there is no case of 5 consecutive consonants(?!.*?[aeiou]{3})
=> To make sure there is no case of 3 consecutive vowels[a-z]*
=> To make sure there are only letters in the inputTry this
<?php
$string = "gkwohtfeiak3d";
function checkstring($input){
if(preg_match("/[0-9]+/", $input, $match) || preg_match("/[AEIOU]{3,}/i", $input, $match)){
if(preg_match("/[0-9]+/", $input, $match)){
echo "Your string has numbers.<br/>";
}
if(preg_match("/[AEIOU]{3,}/i", $input, $match)){
echo "Your string has three or more consecutive vowels";
}
}else{
$checkthis = substr($input, 4);
if(preg_match("/[AEIOU]+/i", $checkthis, $match2)){
echo "$input has vowels after the fourth character";
}else{
echo "$input has no vowels after the fourth character";
}
}
}
checkstring($string);
?>
^(?![aeiou]{3,})(?:\D(?![^aeiou]{4,}[aeiou]*)(?![aeiou]{4,})){3,}$
This regex has all your conditions:
<?php
$values = array(
"test",
"al",
"beautiful",
"eautiful",
"mohsen",
"ehsasssss",
"a test",
"yeeeeee",
"test",
"mohammad",
"ali",
"mohsen",
"mohsenmlpl",
"allllll",
"allilllllo",
"thisisatest",
"zagros",
"4o54o45646o",
"therhythm",
"theg+vowel",
"gbcvcvrt",
"user3109875",
);
foreach ($values as $key => $value)
{
echo "$value => ", preg_match("/^(?![aeiou]{3,})(?:\D(?![^aeiou]{4,}[aeiou]*)(?![aeiou]{4,})){3,}$/", $value)? "TRUE" : "FALSE", "\n";
}
Output:
test => TRUE
al => FALSE
beautiful => TRUE
eautiful => FALSE
mohsen => TRUE
ehsasssss => FALSE
a test => TRUE
yeeeeee => FALSE
test => TRUE
mohammad => TRUE
ali => TRUE
mohsen => TRUE
mohsenmlpl => FALSE
allllll => FALSE
allilllllo => FALSE
thisisatest => TRUE
zagros => TRUE
4o54o45646o => FALSE
therhythm => FALSE
theg+vowel => TRUE
gbcvcvrt => FALSE
user3109875 => FALSE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With