Sunday, December 29, 2024
Google search engine
HomeLanguagesPHP str_replace() Function

PHP str_replace() Function

In this article, we will see how to replace the occurrence of the search string with the replacing string using the str_replace() function in PHP, along with understanding their implementation through the examples.

The str_replace() is a built-in function in PHP and is used to replace all the occurrences of the search string or array of search strings by replacement string or array of replacement strings in the given string or array respectively. 

Syntax

str_replace ( $searchVal, $replaceVal, $subjectVal, $count )

Parameters: This function accepts 4 parameters out of which 3 are mandatory and 1 is optional. All of these parameters are described below: 

  • $searchVal: This parameter can be of both string and array types. This parameter specifies the string to be searched and replaced.
  • $replaceVal: This parameter can be of both string and array types. This parameter specifies the string with which we want to replace the $searchVal string.
  • $subjectVal: This parameter can be of both string and array types. This parameter specifies the string or array of strings which we want to search for $searchVal and replace with $replaceVal.
  • $count: This parameter is optional and if passed, its value will be set to the total number of replacement operations performed on the string $subjectVal.

Return Value: This function returns a string or an array based on the $subjectVal parameter with replaced values.

Approach: If the $searchVal and the $replaceVal arguments are arrays, then all the elements of the $searchVal argument are searched in the $subjectVal string and replaced by the corresponding elements in the $replaceVal argument. If a number of elements in $replaceVal are less than that in $searchVal array, then if there are any occurrences of the additional elements of $searchVal argument in the $subjectVal argument then they will be replaced by an empty string. If the $subjectVal parameter is also an array instead of a string then all of the elements of $subjectVal will be searched.

Consider the below example:  

Input:  $subjectVal  = "It was nice meeting you. May you shine brightly."
        str_replace('you', 'him', $subjectVal)
Output: It was nice meeting him. May him shine brightly.
Explanation: Every occurrence of you is replaced with him.

Input:  $subjectVal  = "You eat fruits, vegetables, fiber every day."
        $searchVal = array("fruits", "vegetables", "fiber")
        $replaceVal = array("pizza", "beer", "ice cream")
        str_replace($array1, $array2, $str)
Output: You eat pizza, beer, ice cream every day.
Explanation: Since both the arguments are arrays, therefore, 
every element from the first argument is replaced with 
the corresponding element from the second argument.

Example 1: The below example illustrate the str_replace() function in PHP.

PHP




<?php
 
  // Input string
  $subjectVal = "It was nice meeting you. May you shine bright.";
 
  // Using str_replace() function
  $resStr = str_replace('you', 'him', $subjectVal);
  print_r($resStr);
?>


Output:

It was nice meeting him. May him shine bright.

Example 2: This example describes replacing all the search strings with replacing strings using the str_replace() function.

PHP




<?php
 
  // Input string
  $str  = "You eat fruits, vegetables, fiber every day.";
 
  // Array containing search string
  $searchVal = array("fruits", "vegetables", "fiber");
 
  // Array containing replace string from  search string
  $replaceVal = array("pizza", "beer", "ice cream");
 
  // Function to replace string
  $res = str_replace($searchVal, $replaceVal, $str);
  print_r($res);
?>


Output:

You eat pizza, beer, ice cream every day.

Referencehttp://php.net/manual/en/function.str-replace.php

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

RELATED ARTICLES

Most Popular

Recent Comments