8 examples of PHP str_replace function to replace strings

The str_replace function

The str_replace function of PHP is used to replace a string with the replacement string.

A simple str_replace example  Replacement count demo

The str_replace PHP function is case sensitive.

If you want to perform case-insensitive replacements then use str_ireplace function (see the last example in this tutorial). First, have a look at the str_replace syntax and examples below.

str_ireplace example Two or more replacement string example

Syntax of using the str_replace function

This is how you may use the str_replace function:

Where:

  • The search_string is the string you want to search in the source string to be replaced. It is also called needle. You may use arrays to specify multiple search strings or needles.
  • The replace_with is the string you want to replace with searched string. There, you may also use arrays for specifying multiple terms.
  • The $source_string is the string you want to perform an action.
  • The $count is an optional parameter that if you pass, it will be set to the total replacements done. Note that, it does not limit the number of replacements.

An example of using the PHP str_replace function

In this example, a simple string is created which is assigned to a variable. The string is:

After that, the str_replace function is used to replace the word “str_replace” by “String replacement”. This is how it is done:

PHP replace

See online demo and code

The following PHP code is executed:

An example of using the HTML tag to highlight replacements

In this example, a string is created where a word “is” used three times. In str_replace function, an HTML <span> tag is added with background color in <style> section, so the replaced words will be highlighted after executing the string replace function. See the example online by clicking the link or image below:

PHP replace HTML

See online demo and code

This is how the string replacement is done:

You can see the complete code including markup and CSS in the demo page.

An example of using count to get total replacements

In certain scenarios, it may be useful to get how many replacements are done by using the str_replace function. In this example, I am using the same code as above, except, passing a variable to get the total count of the replacements. The last line in the output will show the number:

PHP replace count

See online demo and code

This is how the count parameter is used in the function:

echo str_replace(“is”,”<span>was</span>”,$src_rep,$count);

This got the value of total replacements after PHP string replace function is executed. The $count value is displayed by using the echo statement, which is 3.

Specifying an array as needle to replace a string

In some situations, you may need to specify two or more different search terms or needles to replace in the target string. You may do that by using an array in str_replace function.

In this example, an array of two elements is created. The array is used in the string replace method as search string or needle:

PHP str_replace

See online demo and code

This is how the array was created and used in the function:

You see, two words “This” and “function” are replaced by “that” in the target string. However, this is not logical to replace “function” with “that”. It should have its own word to be replaced with.

You may do it by using another array in the replacement_string as shown in the example below.

A demo of using search and replacement arrays in str_replace function

Just extending the above example by creating another array of two elements. This array contains the replacement terms. See the example below:

PHP str_replace arrays

See online demo and code

The following code is used in above example:

You see, the first array, which specifies the search terms in the given string is replaced by the second array elements that are specified in the “replacement string” parameter.

PHP’s str_replace is a case sensitive function?

As mentioned earlier, the PHP str_replace is a case-sensitive function. So “This” has different meanings than “this” while performing replacement by using the str_replace function.

See the following example:

PHP str_replace case sensitive

See online demo and code

In the source string, the word “test” is used with different cases. In the str_replace function, the word “Test” is searched and “TEST” is given as replacement term:

$str_replaced = str_replace(“Test”, “<span>TEST</span>”, $source_string);

You can see, the output is the replacement string where only “Test” occurrence is modified. The word “test” did not change.

Performing case-insensitive replacement by using str_ireplace function

The answer to perform case-insensitive replacement is using the PHP str_ireplace function. The syntax of using the str_ireplace function is just like the str_replace function.

The only difference is the str_ireplace is the case-insensitive version of the str_replace function.

Using the same example as above with str_ireplace function, see the output:

PHP str_ireplace

See online demo and code

This is how the str_ireplace is used:

In the output, you can see that all occurrences of the word “test” are replaced by “TEST”.

An example of user selected values from HTML dropdowns

In this example, two HTML dropdowns are created with pre-defined values. The first is for selecting the search term or needle in the target string. The other is for choosing the replacement term in destinated string.

After selecting two related values e.g.

Press the button to execute the str_replace function of PHP. The values will be submitted by using the “post” method in <form> tag and will be assigned to variables by using $_POST associative array.

The str_replace function will execute and perform the replacement as shown in the example below:

PHP str_replace user values

See online demo and code

You see, the source and replaced strings are shown in the paragraph tags. This is how the markup and PHP code is used in the example.

The markup for the <form> tag:

The PHP code to execute the str_replace function:

Similarly, you may use textboxes or dropdowns with larger lists in some other way to allow users replacing specific terms if you ever come across this type of scenario.