Question

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.

Was it helpful?

Solution

i would use regex also

preg_match('/\w{2,}\s+\w{2,}/u', 'word слово');

\w{2,} matching word character 2 or more. \s+ matching all spaces between and using /u unicode modifier

Edit:

I thought that such solution will help, but you need something more complex like

$text = preg_replace('/\s+/', ' ', 'word w.s');

$parts = explode(' ', $text, 2);
if (count($parts) < 2) {
    throw new \RuntimeException('Should have more than two words');
}

foreach ($parts as $part) {

    preg_match_all('/\w/u', $part, $matches);

    if (count($matches[0]) < 2) {
        throw new \RuntimeException('Should have more than two letters in word');
    }
}

OTHER TIPS

Use Unicode character properties.

\p{L} or \p{Letter} matches a code point with the Letter property in any language. php.net documentation on Unicode character properties

If you are trying to use these words from the strings afterwards regex is not the way to go. Regex is not a parser. The best way I can see doing this is a combination of explode() and ctype_alpha(). Something along the lines of

$prepstring = $string;

//Remove all spaces from the original string and check that everything is a char
if(ctype_alpha(str_replace(array(' '), '', $prepstring))){

  //If everything is a char explode your string into an array
  explode($string);

  if(isset($string[1])){
    //Everything checks out, do something here.
  }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top