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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:32:53+00:00 2026-06-15T06:32:53+00:00

I have an array of words that have to be replaced from a string,

  • 0

I have an array of words that have to be replaced from a string, let’s call it $my_replacements. I also have a string, let’s call it $my_string, that can contain some of those values in.

Right now I have this:

foreach ($my_replacements as $replacement) {
    $replaced_value = preg_replace("/(^|[\n\r\f\t \.\,])" . $replacement . "([\n\r\f\t \.\,]|$)/iu", '', $my_string);
    if($replaced_value !== $my_string) {
       break;
    }
}

And this was good if at the first replacement I wanted to exit the foreach. However, if the strings contains one or more of those values to be replaced it won’t work. How can I use preg_replace to find those words, and replace them all? And it’s important that I know if any replacements have been done.

  • 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-15T06:32:54+00:00Added an answer on June 15, 2026 at 6:32 am

    You can use indexed arrays with preg_replace().

    <?php
    $string = 'The quick brown fox jumped over the lazy dog.';
    $patterns = array();
    $patterns[0] = '/quick/';
    $patterns[1] = '/brown/';
    $patterns[2] = '/fox/';
    $replacements = array();
    $replacements[0] = 'slow';
    $replacements[1] = 'black';
    $replacements[2] = 'bear';
    echo preg_replace($patterns, $replacements, $string);
    ?>
    

    The above example will output:

    The slow black bear jumped over the lazy dog.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seven words in the array: string[7] = {x,x,x,x,x,x,x}; the x is generated
I have an array of tags that I'm pulling from a database, I am
I am trying to replace all words from a text except some that I
I'm attempting to remove noise words from a string, and I have what I
I have an array called $featuresSEO that has a number of words in it
I have a method fetchObjects(String) that is expected to return an array of Contract
Basically I have this string $str=word1 word2 word3; I need array( 'word1', 'word2', 'word3'
I have some checkboxes bound to an array in my model. This works great,
I want to strip whole words from a string wherever they are inside the
I have a 2D array of type Variant . The size and values that

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.