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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:39:39+00:00 2026-06-02T08:39:39+00:00

From time to time I’ll have a string in my project which is simply

  • 0

From time to time I’ll have a string in my project which is simply an acronym (ie. initials) that needs to have all its letters in uppercase form.

eg.

str_replace("Mcg","MCG",$string);

This does the job fine. However, sometimes this string will contain an acronym that is also the start of a word.

eg.

str_replace("Wa","WA",$string)

This will obviously change words like “want” into “WAnt” which I obviously don’t want. Is there some way I can only change words of 2 or 3 characters? I was thinking about exploding the string into an array and going from there. Or is there an easier function I should be utilising?

  • 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-02T08:39:40+00:00Added an answer on June 2, 2026 at 8:39 am

    Use a regex that stops on word boundaries, i.e. something like this:

    preg_replace('/(\b)Wa(\b)/', 'WA', $string);
    

    This replaces all occurrences of ‘Wa’ with ‘WA’, as long as there are spaces, commas, dots, newlines or no characters at all left or right of it (and I’m sure I’m missing a few characters there).

    This is better than filtering on whitespace (or spaces), since you might have the following cases:

    He wants -> He wants
    WaT -> WaT
    Wa -> WA
    Do be do be do Wa. -> Do be do be do WA.
    Some Wa -> Some WA
    Foo Wa Bar -> Foo WA Bar
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From time to time, Oracle is changing the password for my account. My project
I change schema.yml from time to time and execute: symfony propel:build-all-load but the lib/_model
A common question that comes up from time to time in the world of
from time to time (especially when I have a bad connection) I obtain NonRepeatableRequestException
From time to time, I find myself writing server code that produces JavaScript code
From time to time we have to analyze pieces of assembler code (IA32), and
I bump into this from time to time during class design... when I have
If I have a Time object got from : Time.now and later I instantiate
From time to time we all need to print a document from our .NET
From time to time you hear stories that are meant to illustrate how good

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.