I have a small sentence:

$sentence = "I have a Dog.";


$new_sentence = str_replace("Dog", "bird", $sentence );

Is there a way to have 2 or more str_replace() work in that same sentence?

I'm trying to replace "have" to "want". While at the same time replacing "Dog" with "bird".

Do I just need to do multiple str_replace() functions? Or is there a way to do it once?

    http://uk2.php.net/preg-replace

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

      str_replace() can also accept arrays for the search and replace parameters.

      $search = array('quick', 'brown', 'fox');
      $replace = array('slow', 'black', 'bear');
      echo str_replace($search, $replace, $string);
      
        Write a Reply...