PHP str_replace() function


The str_replace() function in PHP is used to replace all occurrences of a substring within a string with another substring. This function is commonly used for string manipulation, such as sanitizing or modifying text.

Syntax:

str_replace(mixed $search, mixed $replace, mixed $subject, int &$count = null): string|array
  • $search: The value(s) to search for (can be a string or an array of strings).
  • $replace: The replacement value(s) (can be a string or an array of strings).
  • $subject: The string or array of strings to perform the replacement on.
  • $count (optional): If provided, this variable will be filled with the number of replacements performed.

Return Value:

  • Returns a string or an array with the replaced values. If the subject is an array, the replacement is performed for each element in the array.

Example 1: Basic String Replacement

<?php $text = "The sky is blue."; $replaced_text = str_replace("blue", "green", $text); echo $replaced_text; ?>

Output:

The sky is green.

Explanation: The word "blue" is replaced with "green", resulting in the new string "The sky is green.".

Example 2: Replacing Multiple Substrings

<?php $text = "I like apples and oranges."; $replaced_text = str_replace(["apples", "oranges"], ["bananas", "grapes"], $text); echo $replaced_text; ?>

Output:

I like bananas and grapes.

Explanation: Both "apples" and "oranges" are replaced with "bananas" and "grapes", respectively. You can replace multiple substrings by passing arrays to the $search and $replace parameters.

Example 3: Case-Sensitive Replacement

<?php $text = "Hello World"; $replaced_text = str_replace("world", "PHP", $text); echo $replaced_text; ?>

Output:

Hello World

Explanation: The replacement did not occur because str_replace() is case-sensitive. To perform a case-insensitive replacement, use str_ireplace().

Example 4: Counting the Number of Replacements

<?php $text = "I have a cat. My cat is cute."; $replaced_text = str_replace("cat", "dog", $text, $count); echo $replaced_text; echo "\nNumber of replacements: " . $count; ?>

Output:

I have a dog. My dog is cute. Number of replacements: 2

Explanation: The word "cat" is replaced twice with "dog", and the $count parameter holds the number of replacements made.

Example 5: Replacing in an Array

<?php $fruits = ["apple", "banana", "orange"]; $replaced_fruits = str_replace("apple", "kiwi", $fruits); print_r($replaced_fruits); ?>

Output:

Array ( [0] => kiwi [1] => banana [2] => orange )

Explanation: The word "apple" is replaced with "kiwi" in the first element of the array, while the other elements remain unchanged.

Example 6: Replacing with an Empty String

<?php $text = "Hello, World!"; $replaced_text = str_replace("World", "", $text); echo $replaced_text; ?>

Output:

Hello, !

Explanation: The word "World" is removed by replacing it with an empty string "". This technique can be used to delete specific substrings from a string.


Key Points:

  • str_replace() is case-sensitive. If you want a case-insensitive replacement, use str_ireplace().
  • You can replace multiple different substrings at once by passing arrays to the $search and $replace parameters.
  • The optional $count parameter will keep track of how many replacements were made.
  • This function works on both strings and arrays.

str_replace() is very useful for modifying or cleaning up strings, such as replacing words in text, removing unwanted characters, or formatting output.