How do I replace certain parts of my string?

str_replace is sufficient for simple replacement jobs (such as replacing a single letter), but the use of preg_replace is generally advised (if you want something more flexible or versatile), because it's flexible and versatile. And as the 'a' is just an example...:

$String = preg_replace('/<string>/','<replacement>',$String);

Or if you want multiple replacements at once:

$String = preg_replace(array('/<string 1>/','/<string 2>/'),array('<replacement 1>','<replacement 2>'),$String);

preg_replace can, unfortunately, be quite tricky to use. I recommend the following reads: http://php.net/manual/en/function.preg-replace.php http://www.phpro.org/tutorials/Introduction-to-PHP-Regex.html

Also, if you decide to use str_replace(), and your replacement needs to be case-sensitive, you're going to need str_ireplace().


Search & Replace

There are a few different functions/methods to replace a certain part of a string with something else, all with their own advantages.


##str_replace() method (binary safe; case-sensitive)

Arguments

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

str_replace() has 3 required arguments as you can see in the above definition with the correct order, all of which can take a string as also an array as argument!

Search & Replace

  • search(string) AND replace(string) → Replaces the search string with the replace string.

  • search(array) AND replace(string) → Replaces all search elements with the replace string.

  • search(string) AND replace(array) → Throws you a notice: "Notice: Array to string conversion", because a replacement array for just one search string doesn't make sense, so it tries to convert the array to a string.

  • search(array) AND replace(array) → Replaces each search element with the corresponding replace element (Keys are ignored!).

  • search(more elements) AND replace(less elements) → Replaces each search element with the corresponding replace element (For the missing replace elements an empty string will be used).

  • search(less elements) AND replace(more elements) → Replaces each search element with the corresponding replace element (Unneeded replace elements are ignored).

Subject

  • subject(string) → Replacement is done for the subject string.

  • subject(array) → Replacement is done for each array element.

Code

echo str_replace("search", "replace", "text search text");
echo str_replace(["t", "a"], "X", "text search text");
echo str_replace("search", ["replace1", "replace2"], "text search text");
echo str_replace(["a", "c"], ["X", "Y"], "text search text");

Output

text replace text
XexX seXrch XexX
Notice: Array to string conversion
text seXrYh text

Notes

  1. Gotcha!

Important to know is that str_replace() works from left to right of the array. This means it can possible replace a value which you already replaced. For example:

    echo str_replace(array("a", "b"), array("b", "c"), "aabb");
    //Probably expected output: bbcc
    //Actual output:            cccc
  1. Case insensitive

If you want to make the search case insensitive you can use str_ireplace() (Notice the i for case-insensitive).

  1. Multidimensional array

str_replace()/str_ireplace() does NOT work for multidimensional arrays. See this manual comment for such an implementation. Of course you can also replace str_replace() with str_ireplace() for case-insensitive.

If you want to put everything together and create a function that also works for multidimensional arrays case-insensitive you can do something like this:

<?php 
function str_ireplace_deep($search, $replace, $subject) 
{ 
if (is_array($subject)) 
{ 
    foreach($subject as &$oneSubject) 
        $oneSubject = str_ireplace_deep($search, $replace, $oneSubject); 
    unset($oneSubject); 
    return $subject; 
} else { 
    return str_ireplace($search, $replace, $subject); 
} 
} 
?>

##strtr() method (50% binary safe; case-sensitive)

Arguments

string strtr ( string $str , string $from , string $to )

string strtr ( string $str , array $replace_pairs )

The function either takes 3 arguments with a from and to string or it takes 2 arguments with a replacement array array("search" => "replace" /* , ... */), all of which you can see in the above definition with the correct order.

2 Arguments

It starts to replace the longest key with the corresponding value and does this until it replaced all key => value pairs. In this case the function is binary safe, since it uses the entire key/value.

3 Arguments

It replaces the from argument with the to argument in the subject byte by byte. So it is not binary safe!

If the from and to arguments are of unequal length the replacement will stop when it reaches the end of the shorter string.

Subject

It doesn't accept an array as subject, just a string.

Code

echo strtr("text search text", "ax", "XY");;
echo strtr("text search text", ["search" => "replace"]);

Output

teYt seXrch teYt
text replace text

Notes

  1. Gotcha!

Opposed to str_replace(), strtr() does NOT replace replaced strings. As an example:

    echo strtr("aabb", ["a" => "b", "b" => "c"]);
    //If expecting to replace replacements: cccc
    //Actual output:                        bbcc

Also if you want to replace multiple things with the same string you can use array_fill_keys() to fill up your replacement array with the value.

  1. Case insensitive

strtr() is NOT case-insensitive NOR is there a case-insensitive equivalent function. See this manual comment for a case-insensitive implementation.

  1. Multidimensional array

strtr() does opposed to str_replace() NOT work with arrays as subject, so it also does NOT work with multidimensional arrays. You can of course use the code above from str_replace() for multidimensional arrays and just use it with strtr() or the implementation of stritr().

If you want to put everything together and create a function that also works for multidimensional arrays case-insensitive you can do something like this:

<?php
if(!function_exists("stritr")){
function stritr($string, $one = NULL, $two = NULL){
/*
stritr - case insensitive version of strtr
Author: Alexander Peev
Posted in PHP.NET
*/
    if(  is_string( $one )  ){
        $two = strval( $two );
        $one = substr(  $one, 0, min( strlen($one), strlen($two) )  );
        $two = substr(  $two, 0, min( strlen($one), strlen($two) )  );
        $product = strtr(  $string, ( strtoupper($one) . strtolower($one) ), ( $two . $two )  );
        return $product;
    }
    else if(  is_array( $one )  ){
        $pos1 = 0;
        $product = $string;
        while(  count( $one ) > 0  ){
            $positions = array();
            foreach(  $one as $from => $to  ){
                if(   (  $pos2 = stripos( $product, $from, $pos1 )  ) === FALSE   ){
                    unset(  $one[ $from ]  );
                }
                else{
                    $positions[ $from ] = $pos2;
                }
            }
            if(  count( $one ) <= 0  )break;
            $winner = min( $positions );
            $key = array_search(  $winner, $positions  );
            $product = (   substr(  $product, 0, $winner  ) . $one[$key] . substr(  $product, ( $winner + strlen($key) )  )   );
            $pos1 = (  $winner + strlen( $one[$key] )  );
        }
        return $product;
    }
    else{
        return $string;
    }
}/* endfunction stritr */
}/* endfunction exists stritr */

function stritr_deep($string, $one = NULL, $two = NULL){
if (is_array($string)) 
{ 
    foreach($string as &$oneSubject) 
        $oneSubject = stritr($string, $one, $two); 
    unset($oneSubject); 
    return $string; 
} else { 
    return stritr($string, $one, $two); 
} 

}
?>

##preg_replace() method (binary safe; case-sensitive)

Arguments

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

preg_replace() has 3 required parameters in the order shown above. Now all 3 of them can take a string as also an array as argument!

Search & Replace

  • search(string) AND replace(string) → Replaces all matches of the search regex with the replace string.

  • search(array) AND replace(string) → Replaces all matches of each search regex with the replace string.

  • search(string) AND replace(array) → Throws you a warning: "Warning: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array", because a replacement array for just one search regex doesn't make sense.

  • search(array) AND replace(array) → Replaces all matches of each search regex with the corresponding replace element(Keys are ignored!).

  • search(more elements) AND replace(less elements) → Replaces all matches of each search regex with the corresponding replace element(For the missing replace elements an empty string will be used).

  • search(less elements) AND replace(more elements) → Replaces all matches of each search regex with the corresponding replace element(Unneeded replace elements are ignored).

Subject

  • subject(string) → Replacement is done for the subject string.

  • subject(array) → Replacement is done for each array element.

Please note again: The search must be a regular expression! This means it needs delimiters and special characters need to be escaped.

Code

echo preg_replace("/search/", "replace", "text search text");
echo preg_replace(["/t/", "/a/"], "X", "text search text");
echo preg_replace("/search/", ["replace1", "replace2"], "text search text");
echo preg_replace(["a", "c"], ["X", "Y"], "text search text");

Output

text replace text
XexX seXrch XexX
Warning: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
text seXrYh text

Notes

  1. Gotcha!

Same as str_replace(), preg_replace() works from left to right of the array. This means it can possible replace a value which you already replaced. For example:

    echo preg_replace(["/a/", "/b/"], ["b", "c"], "aabb");
    //Probably expected output: bbcc
    //Actual output:            cccc
  1. Case insensitive

Since the search argument is a regular expression you can simply pass the flag i for case-insensitive search.

  1. Multidimensional array

preg_replace() does NOT work for multidimensional arrays.

  1. Backreference

Be aware that you can use \\n/$n as backreference to your capturing groups of the regex. Where 0 is the entire match and 1-99 for your capturing groups.

Also if the backreference is immediately followed by a number you have to use \${n}.

  1. Replacement / "The /e modifier is deprecated"

The replacement in preg_replace() can't use callback functions as replacements. So you have to use preg_replace_callback(). Same when you use the modifier e and get "Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead". See: Replace preg_replace() e modifier with preg_replace_callback

If you want to put everything together and create a function that also works for multidimensional arrays case-insensitive you can do something like this:

<?php
function preg_replace_deep($search, $replace, $subject) 
{ 
if (is_array($subject)) 
{ 
    foreach($subject as &$oneSubject) 
        $oneSubject = preg_replace_deep($search, $replace, $oneSubject); 
    unset($oneSubject); 
    return $subject; 
} else { 
    return preg_replace($search, $replace, $subject); 
} 
} 
?>

##Loops while / for / foreach method (NOT binary safe; case-sensitive)

Now of course besides all of those functions you can also use a simple loop to loop through the string and replace each search => replace pair which you have.

But this gets way more complex when you do it binary safe, case-insensitive and for multidimensional arrays than just using the functions above. So I won't include any examples here.



Affected String

Right now all methods shown above do the replacement over the entire string. But sometimes you want to replace something only for a certain part of your string.

For this you probably want to/can use substr_replace(). Or another common method is to use substr() and apply the replacement only on that particular substring and put the string together afterwards. Of course you could also modify your regex or do something else to not apply the replacement to the entire string.


strtr ($str, array ('a' => '<replacement>'));

Or to answer your question more precisely:

strtr ("Hello, my name is Santa", array ('a' => '<replacement>'));