Published on : August 05, 2020
Title : php.str_replace
Purpose : It replaces some characters with some other characters in a string. This function works by the following rules:

  • If the string to be searched is an array, it returns an array
  • If the string to be searched is an array, find and replace is performed with every array element
  • If both find and replace are arrays, and replace has fewer elements than find, an empty string will be used as replace
  • If find is an array and replace is a string, the replace string will be used for every find value

Note: This function is case-sensitive. Use the str_ireplace() function to perform a case-insensitive search.
Syntax : [php.str_replace p1=’find’ p2=’replace’ p3=’string’ p4=’count’ o.set=variable name /]
Input Parameters :
p1 : Required. Specifies the value to find
p2 : Required. Specifies the value to replace the value in find
p3 : Required. Specifies the string to be searched
p4 : Optional. A variable that counts the number of replacements
Return Value : Returns a string or an array with the replaced values
Example 1 :
Code : [php.str_replace p1=’ ‘ p2=’-‘ p3=’H e l l o W o r l d!’ o.set=template.result/]
Output : string(20) “H-e-l-l-o-W-o-r-l-d!”
Description : It replaced the space with hyphen and returned the string
Example 2 :
Code : [template.set_array array]
    [one]one[/one]
    [two]two[/two]
    [three]three[/three]
    [four]four[/four]
[/template.set_array]
[php.str_replace p1=’one’ p2=’zero’ p3=”{template.array}” o.set=template.result/]
[template.result.echo /]
Output : “one” => string(4) “zero”
“two” => string(3) “two”
“three” => string(5) “three”
“four” => string(4) “four”
Description : It replaced the element one with zero and returned an array