I would like to make function which detects/validates that a string got at least 2 words, and each word has at least 2 letters (except for the two letters, it can contain any other char {without numbers}, but I don’t care which and how many).
Now, I am not sure if I should use regex for this or I can do it in other ways.
If I need to make regex for it, I also dont know how to do it because I need to check all the letters available.
This is the regex I got now [A-Za-z]{2,}(\s[A-Za-z]{2,}) which validates 2 words and 2 letters at least in each word.
EDIT:
After re-thinking I decided to support most languages since kr-jp-cn languages work differently than rest of languages. My main rules won’t let kr-jp-cn letters count as letters but as chars.
EDIT2:
This is the function I’m using based on @message answer.
function validateName($name)
{
if (strcspn($name, '0123456789') == strlen($name)) //return the part of the string that dont contain numbers and check if equal to it length - if it equal than there are no digits - 80% faster than regex.
{
$parts = array_filter(explode(' ',$name)); //should be faster than regex which replace multiple spaces by single one and then explodes.
$partsCount = count($parts);
if ($partsCount >= 2)
{
$counter = 0;
foreach ($parts as $part)
{
preg_match_all('/\pL/u', $part, $matches);
if (count($matches[0]) >= 2)
{
$counter++;
}
}
}
if ($counter == $partsCount)
{
return 'matches';
}
}
return 'doesnt match';
}
Thanks for the help.
i would use regex also
\w{2,}matching word character 2 or more.\s+matching all spaces betweenand using
/uunicode modifierEdit:
I thought that such solution will help, but you need something more complex like