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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:08:02+00:00 2026-06-07T17:08:02+00:00

I am using preg_replace($oldWords, $newWords, $string); to replace an array of words. I wish

  • 0

I am using preg_replace($oldWords, $newWords, $string); to replace an array of words.

I wish to replace all words starting with foo into hello, and all words starting with bar into world

i.e foo123 should change to hello , foobar should change to hello, barx5 should change to world, etc.

If my arrays are defined as:

$oldWords = array('/foo/', '/bar/');  
$newWords = array('hello', 'world');

then foo123 changes to hello123 and not hello. similarly barx5 changes to worldx5 and not world

How do I replace the complete matched word?

Thanks.

  • 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-07T17:08:05+00:00Added an answer on June 7, 2026 at 5:08 pm

    This is actually pretty simple if you understand regex, as well as how preg_replace works.

    Firstly, your replacement arrays are incorrectly formed. What is:

    $oldWords = array('\foo\', '\bar\');
    

    Should instead be:

    $oldWords = array('/foo/', '/bar/');
    

    As the backslash in php escapes the character after it, meaning your strings were getting turned into non-strings, and it was messing up the rest of your code.

    As to your actual question, however, you can achieve the desired effect with this:

    $oldWords = array('/foo\w*/', '/bar\w*/');
    

    \w matches any word character, while * is a quantifier either meaning 0 or any number of matches.

    Adding in those two items will cause the regex to match any string with foo and x number of word-characters directly after it, which is what preg_replace then replaces; the match.

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

Sidebar

Related Questions

I'd like to replace the string {{edit(3)}} with open3close using preg_replace
How can I replace Å with some other string using preg_replace? Thanks!
Possible Duplicate: Replace only first match using preg_replace I have a string as follows:
Possible Duplicate: replace all “foo” between () Hello, I tried to use regex to
Using preg_replace() , I want to replace a string like this... aaabbbccc => aaa******
what is the best way to replace a string like this using preg_replace: <a
Similar to How replace all spaces inside HTML elements with &nbsp; using preg_replace? Except
I am having an issue removing %3Cbr+%2F%3E from my string using the preg_replace function.
I am using preg_replace to strip unwanted characters from a string. Thats working fine.
I am using preg_replace to remove some content from a string. What I am

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.