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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:05:32+00:00 2026-06-10T18:05:32+00:00

The function str_replace() accepts an array as its first parameter, but I don’t see

  • 0

The function str_replace() accepts an array as its first parameter, but I don’t see any way to store the string for use in the second parameter where the found text is replaced. For instance, if I want to format around the string with brackets or quotation marks.

Here’s an example. Say I was looking to insert quotation marks around the words in $array when they’re spit back out or change the case of the word to from all lowercase to the case set in $array:

$array = array('Over','Dog','Quick');

$string = "The quick brown fox jumps over the lazy dog";

$string = explode(" ",$string);

foreach ($string as $word) {

    echo str_replace($array, "???", $word);

    echo " ";

}

That will return The ??? brown fox jumps ??? the lazy ???. I realize this might be a job for regex, but I haven’t been able to figure out how to get it to handle both formatting and replacing. I feel like I’m missing something here, but nothing I google even touches this subject.

  • 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-10T18:05:33+00:00Added an answer on June 10, 2026 at 6:05 pm
    $array = array('Over', 'Dog', 'Quick');
    
    $string = "The quick brown fox jumps over the lazy dog";
    
    echo str_ireplace($array, $array, $string);
    

    See it here in action: http://viper-7.com/q7n7uw


    So, for example, if you want to put quotation marks around the string, use this:

    $array = array('Over', 'Dog', 'Quick');
    $replacements = array();
    foreach($array as $item) $replacements[] = '"' . $item . '"';
    
    $string = "The quick brown fox jumps over the lazy dog";
    
    echo str_ireplace($array, $replacements, $string);
    

    See here: http://viper-7.com/2afgL5


    In reality though, this is a job for preg_replace:

    $search = array('Over','Dog','Quick');
    
    foreach($search as &$item) $item = '~\b(' . $item . ')\b~i';
    
    $string = "The quick brown fox jumps over the lazy dog";
    
    echo preg_replace($search, '"$1"', $string);
    

    See here: http://viper-7.com/nAlqzX

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

Sidebar

Related Questions

Is there any way to replace titles faster then str_replace function? I've got a
I face a problem with the str_replace function, see the code below : $query
How is this possible that the str_replace() function below returns biWeekly and not Every
I have this function: function validate($data) { $newData = str_replace( , , $newData); $newData =
function uncompress($srcName, $dstName) { $sfp = gzopen($srcName, rb); $dstName = str_replace('.gz', '', $dstName); $fp
I have the following situation: function Mach3Code(Str: String): String; var StrOut: String; begin StrOut
In PHP, there is a str_replace function that basically does a find and replace.
Coming from a PHP background, I am a little spoiled with the str_replace function
function trim(str,options){ var string = str.replace(/^\s\s*/, ''), ws = /\s/, i = str.length, j
I want to capitalise the first letter of a string. That part is done.

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.