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

  • Home
  • SEARCH
  • 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 9170971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:05:59+00:00 2026-06-17T16:05:59+00:00

I am just writing some code that will analyze a users name. I have

  • 0

I am just writing some code that will analyze a users name. I have so far written the code to detect if the users name contains any bad words, punctuation, symbols, numbers or repeating characters, but managed to get myself in a pickle over detection of unusual capitalization.

So far i have put in place a very simple way or doing this in the form of; If the name has any more than two capital letters we reject the name. (I used two as some people have double-barreled names, e.g. Anne-marie).

$capitals = strlen($name) - strlen(preg_replace('/[A-Z]/', '', $name));
    if ($capitals > 2) {
        $hasError = true; 
}

Although it gives us a half-baked solution to the unusual capitalization issue. The problem is that it is, Half-baked! It still allows many possible patters of upper and lowercase letters in a name, just not if they exceed two characters.

So, i have added in an extra piece of code in the if statement to check for repeating upper-case characters, thus making the above code now this:

$capitals = strlen($name) - strlen(preg_replace('/[A-Z]/', '', $name));
    if (preg_match('/[A-Z]{2,}/', $name) || $capitals > 2) {
        $hasError = true; 
}

Now this seems to have solved 70% of this issue. Users can now no longer use patterns like these. XXxxx, XxXxX, xxxXX, xxx-XXxx. But there is still problems i need to address, as long as they use less than two chars, and don’t group capital-letters they can still create their “cool” looking names. So, if a user was to input a name styled like JeSse it would be accepted.

So my question is how would i go about the last step of this issue? I need to only allow users to have their first names in the formats Jesse, Jesse-James, Jesse James.
How can i make sure only the first letter of their name is capitalized, even if their name is double barreled?

you see in python i would use .find() and just detect the first letter of the first word and make sure it’s upper-case, count on until we meet a space or hyphen, then make sure the next letter after the space or hyphen is also upper-case? But i have no clue of how to do this with regex in PHP.

Would this be the right way to do it with regex? if it is, how would i go about this? or does PHP have a secret .find() function i can use in a similar way to pythons? and if it does, would it be more appropriate to go down that route?

Sorry i went into so much detail, seems to be to many numptys putting up questions like “I have a regex issue, i need to detect patterns” then expect back an answer that will be useful to them”. I wanted to provide enough information for it to be useful to people in the future landing on this page.

Many thanks for all future replies.

Jamie

P.S. Just out of interest, does anyone also know how to use non-English characters in PHP. Would i need to create a string of the characters i wish to detect, or does php have a ‘code’ for each character like html entities?

  • 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-17T16:06:00+00:00Added an answer on June 17, 2026 at 4:06 pm

    You can probably do that all in one regex:

     /^ [A-Z][a-z]+ ( [-\s.] [A-Z][a-z]+ )? $/x
    

    Which would allow for Jesse or Jesse-James or Jesse.James or Jesse James only. (Remove dot and \space if you don’t want those.)

    If you want to allow the second part to start with lowercase, or ensure at least two lowercase letters follow each other use {2,} in place of +:

     /^ [A-Z][a-z]{2,} ( [-\s.] [A-Z]? [a-z]{2,} )? $/x
                                  ^
                              optional now
    

    For unicodeness use \p{Lu} for uppercase letters.
    And \p{Ll} for lowercase ones:

     /^ \p{Lu}\p{Ll}+ ( [-\s.] \p{Lu} ? \p{Ll}+ )? $/xu
    

    You might wish to add another optional for allowing Jesse-J.-James for example, thus having a single letter abbreviation with:

     ([A-Z] [.] [-\s])?
    

    Though that might need to be repeated at the start and middle part.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some code (just for fun so far) in Python that will store
I'm writing some logging/auditing code that will be running in production (not just when
I am writing some code that traverses a structure that may have cyclic references.
I've written some code that contains a main and a number of subclasses that
I'm writing code that will, when a button is clicked, display some simple text
I'm writing some code to determine the name that an object is assigned to.
I am writing about code that I have written for documentation, mainly in PHP.
If you are writing some SIMD code that will be run by another program,
I'm writing some code that uses a Tree (a regular tree that can have
Just putting part of some code here where I am writing two values to

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.