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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:46:57+00:00 2026-06-17T08:46:57+00:00

I have set up a very simple code snippet: $string = ‘Some random words.

  • 0

I have set up a very simple code snippet:

$string = 'Some random words. Some more random, very random words.';
$words = explode(" ", $string);

for ($i = 0; $i < count($words); $i++) {
    $word = $words[$i];
    $words[$i] = str_replace(".", "!", $word);
    $words[$i] = str_replace(",", "?", $word);
}

print_r($words);

The output is this:

Array
(
    [0] => Some
    [1] => random
    [2] => words.
    [3] => Some
    [4] => more
    [5] => random?
    [6] => very
    [7] => random
    [8] => words.
)

Why only the second str_replace() function affect the string? If I remove the second str_replace() the first one works perfectly. It’s not about usage of str_replace() but I believe me doing something very very simply wrong.

By the way – I am aware of preg_replace() and passing an array to str_replace() but would like to hear about this particular situation :).

EDIT:
Thank you all for blazing quick responses. I am in a shame for such an issue but it really didn’t catch my eyes at first. Thanks everyone! I will accept the first correct answer by Mike Brant.

  • 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-17T08:46:57+00:00Added an answer on June 17, 2026 at 8:46 am

    It is because your second statement uses $word as the subject of replacement and not $words[$i] which was where you assigned the string after the first replacement.

    You can fix by either working directly with $words[$i] the entire time, or working exclusively with your temp variable and then making assignment like this:

    for ($i = 0; $i < count($words); $i++) {
        $word = $words[$i];
        $word = str_replace(".", "!", $word);
        $words[$i] = str_replace(",", "?", $word);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple line of code that set and read a cookie.
I have a very simple .htaccess file set up to redirect a 404 to
I have a very simple script: #!/bin/bash gnuplot << EOF set term postscript portrait
This is the very simple code I am using to set a cookie in
I have a very simple xmlhttp snippet that i need to convert to php.
I have a very simple CSS related question on iPad. I have a set
Currently I have a very simple tab system set up, the problem is when
A while back I set up the following to have a very simple image
am doing something very very simple. I have a listbox whose events are set
I am having a strange issue. I have a very simple piece of code

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.