Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8314683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:54:12+00:00 2026-06-08T20:54:12+00:00

I would like to make function which detects/validates that a string got at least

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:54:15+00:00Added an answer on June 8, 2026 at 8:54 pm

    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');
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to make a function that takes in a list of integers
I would like to make a ListBox function like a Grid . Each time
I would like to make a function that will create objects that have a
I would like to make a function which does the following if (vim is
I would like to make a checkbox form that has both mouse over image
I would like make an extension method for the generic class A which takes
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to make a small silverlight app which displays one fairly large
I would like to make my iframe fadein as soon as the visitor has
I'm using canvas to draw a chart. I would like make it animated, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.