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 9151933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:55:18+00:00 2026-06-17T11:55:18+00:00

Using PHP’s preg_match() function, how would I go about matching words that are between

  • 0

Using PHP’s preg_match() function, how would I go about matching words that are between 2 and 5 characters? In this case the letters are guaranteed to be uppercase A-Z and there is only one word in each $word variable.

It must reject a word of 6 characters:

preg_match("/[A-Z]{2,5}/", $word); 

…does not appear to work, presumably because a 6 character $word will match based on the first 2-5 characters.

This is a simplified version of my actual problem which cannot be solved with a strlen() test.

  • 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-17T11:55:19+00:00Added an answer on June 17, 2026 at 11:55 am

    Your post was not clear if you wanted the string to be exactly A-Z between 2 and 5 characters. This will match all words in a string between 2-5 characters:

    preg_match('/\b[A-Z]{2,5}\b/', $string, $matches);
    var_dump($matches);
    

    If you just the string to be exactly between 2 and 5 characters and only consisting of A-Z:

    preg_match('/^[A-Z]{2,5}$/', $word);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using PHP and MySQL, I have a query that will look something like this:
Using PHP, is there a function/method/way to check if a variable contains something that
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Using PHP , I would like to make a while loop that reads a
Using PHP I echo out table rows in a loop like this: <?php /*
Using PHP I would like to be able to list root's crontab. I am
Using PHP, How to identify that the string is a path to any file?
Using PHP.. I have a small app that I built that currently uses a
Using PHP, I have a simple database that may store multiple items with the
Using PHP 5.2.6 in XAMPP : I read about sql injections here and tried

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.