PHP str_replace() Function

Learn via video courses
Topics Covered

Overview

str_replace is a versatile string manipulation function in PHP used to replace occurrences of a specified substring with another in a given string. With a simple syntax, it helps modify strings by globally substituting all instances of the search term.

The function accepts arrays for both search and replacement, allowing batch replacement of multiple substrings. It is invaluable for text manipulation tasks such as formatting, data cleaning, and dynamic content generation. However, it is case-sensitive by default, and for case-insensitive replacements, developers may need to use additional functions or flags. str_replace empowers developers to efficiently transform strings while streamlining processes that involve text alterations.

Syntax of str_replace in PHP

Here is the syntax of str_replace in PHP:

  • $search (required): This parameter specifies the substring to be searched for within the $subject string. It can be a single string or an array of strings to search for.
  • $replace (required): This parameter defines the string(s) that will replace the occurrences of the $search substring in the $subject string. It can be a single string or an array of strings. The replacement occurs in the same order as the search strings provided.
  • $subject (required): The original string where replacements will be made.
  • &$count (optional): This is a reference parameter that stores the number of replacements that were performed. If specified, it will be updated with the count of replacements made. This parameter is optional.

Parameters of str_replace in PHP

  1. $search (required):
  • Type: String or Array of strings
  • Description: Specifies the substring(s) to be searched for within the $subject string. It can be a single string or an array of strings. If an array is provided, each element is treated as a separate search string.
  • Example: "world", ["apple", "banana"]
  1. $replace (required):
  • Type: String or Array of strings
  • Description: Defines the replacement string(s) that will replace the occurrences of the $search substring(s) in the $subject string. It can be a single string or an array of strings. The replacements occur in the same order as the search strings provided.
  • Example: "universe", ["fruit", "fruit"]
  1. $subject (required):
  • Type: String
  • Description: The original string where replacements will be made.
  • Example: "Hello, world! Hello, PHP!"
  1. &$count (optional):
  • Type: Integer (reference)
  • Description: This optional parameter is a reference variable that will hold the count of replacements made.
  • Example: $count = 0;

Return Value of str_replace in PHP

The str_replace function in PHP returns a modified string or an array of strings after performing the specified replacements. The return value depends on the data type of the $search and $replace parameters:

  1. If both $search and $replace are strings:
  • The function returns a new string where all occurrences of the $search substring in the $subject string have been replaced with the $replace string.
  1. If both $search and $replace are arrays of strings:
  • The function returns an array of strings where each element of the array corresponds to the $subject string with replacements applied according to the order of elements in the $search and $replace arrays.
  1. If $search is an array and $replace is a string:
  • The function performs replacements on each element of the $search array using the same $replace string, and it returns an array of strings with the replacements.

Examples

Using str_replace() with an array and a count variable

Output

  • The $originalString contains the text where replacements need to be performed.
  • The $search array contains the substrings "sun," "sky," and "birds" that we want to search for in the original string.
  • The $replace string is set to "nature," which will be used to replace occurrences of the substrings in the $search array.
  • The $count variable is initialized to 0. It will be used to store the number of replacements made during the operation.
  • The str_replace() function is called with the search,search, `replace, and $originalString parameters, along with the reference to the $count` variable. The function returns the modified string with replacements.
  • The modified string is then echoed to the output, and the count of replacements stored in the $count variable is displayed. Run the above code in your editor for a better and clearer explanation.

sing str_replace() with fewer elements in replace than find

Output

  • The $originalString contains the text where replacements need to be performed.
  • The $search array contains the substrings "apples," "red," and "green" that we want to search for in the original string.
  • The $replace array contains a single element "oranges," which will be used as a replacement for occurrences of the find elements.
  • The str_replace() function is called with the $search, ``$replace, and $originalString parameters. Since there are fewer elements in the $replace array than in the $search array, the "oranges" replacement will be applied to each corresponding occurrence of the find elements.
  • The modified string is then echoed to the output. Run the above code in your editor for a better and clearer explanation.

Replacing all the search strings with replacing strings using the str_replace() function

Output

  • The $originalText variable contains the text where replacements need to be performed.
  • The $search array contains the substrings "quick," "fox," "lazy," and "dog" that we want to search for in the original text.
  • The $replace array contains the corresponding replacement strings "fast," "cat," "active," and "puppy."
  • The str_replace() function is called with the $search, $replace, and $originalText parameters. It scans through the original text and replaces each occurrence of a search string with its corresponding replacement string from the arrays.
  • The modified text is then echoed to the output. Run the above code in your editor for a better and clear explanation.

Conclusion

  • str_replace allows developers to replace occurrences of one or more substrings within a given string with specified replacement strings.
  • It supports replacing multiple occurrences of different substrings simultaneously, enhancing efficiency and readability in code.
  • Both search and replacement parameters can be arrays, enabling bulk replacements across the entire string.
  • By default, str_replace is case-sensitive, but developers can opt for case-insensitive replacements using other functions like str_ireplace.
  • Developers can control the number of replacements made using an optional count parameter, providing fine-tuned control over the operation.
  • str_replace finds use in dynamic content generation, template rendering, data cleansing, and text formatting within web development projects.
  • It facilitates user-friendly interfaces by allowing dynamic modifications to text content based on user inputs and preferences.