PHP Replace String


StringViews 1437

In PHP, we can replace a string or an array of string with another string or an array of string using str_replace. In this article, we will explain about PHP replace string operation.

Syntax of PHP replace String

str_replace(find,replace,string,count)

This method uses 4 parameters as described below:

find – This is the value to find in the input string which is mandatory.

replace – This is the value to replace based on the find parameter which is a mandatory parameter.

string – This is the input value which is mandatory. It can be a string or an array of string

count – This is an optional field that counts the number of replacements.

Return value – String or an array of string with replaced values.

Functionality of str_replace method

PHP replace string works based on the below input value combinations:

  • If the input field and both find and replace value is a string, then replace value will replace the find value in the input string based on the number of occurrences.
  • If the input field is an array and find and replace value is a string, then every find element in the array will be replaced with the replace value
  • When both find and replace values are array, then every element of find array will be searched in the input string and replaced with every element in the replace array.
  • When replace array has fewer elements than find array, the remaining find elements will be replaced with an empty string.

str_replace method is case sensitive.

For eg: input string: “Hello world”

find value: hello

replace value: welcome

In this case, find value will not get replaced since there is a capital case mismatch in the first letter.

Example of replacing a single string

Below is a simple example, where we search for the word “Java” in the input string and replace it with the word “PHP””.

<html>
<body>


<?php
$string = "Welcome to Java tutorial";
echo str_replace("Java","PHP",$string);
?>

</body>
</html>
Output:
Welcome to PHP tutorial

Example of replacing string in an array input

In the below example, the input is an array of strings. We search for the string element “java” and replace all the occurrences of the search string with “javascript””. Since we have passed the last parameter, it prints the count of replacements.

<html>
<body>

<?php
//Input array
$arr = array("java","php","C","C++","java");
//Search string
$find = "java";
//Replace string
$replace = "javascript";
print_r(str_replace($find,$replace,$arr,$i));
echo "<br>" . "Replacements: $i";
?>

</body>
</html>
Output:
Array ( [0] => javascript [1] => php [2] => C [3] => C++ [4] => javascript )
Replacements: 2

Example of replace when the search parameter is an array

Here, we have both find and replace values as an array with an equal number of strings. Hence each search element is replaced by replace element.

<html>
<body>

<?php
//Input string
$string = "It is cold outside";
//Search values
$find = array("cold","outside");

//Replace values
$replace = array("hot","inside");

$output = str_replace($find,$replace,$string);
print_r($output);

?>

</body>
</html>
Output:
It is hot inside

Example of replace when replace array is less than find array

In the below example, we have 2 strings in find array and only 1 string in replace array. Hence the 2nd find array element will be replaced with an empty string. Hence the word “outside” is not printed in the output.

<html>
<body>


<?php
//Input string
$string = "It is cold outside";
//Find array
$find = array("cold","outside");
//Replace array
$replace = array("hot");

$output = str_replace($find,$replace,$string);
print_r($output);

?>

</body>
</html>
Output:
It is hot

Conclusion

In this tutorial, you have learned about how to use str_replace with various functionality combinations for php replace string.

Reference:

https://www.php.net/manual/en/function.str-replace.php

Translate ยป