what is the syntax of the STR_REPLACE() function in php?
str_replace(find,replace,string,count)
Parameter | Description |
---|---|
find | Required. Specifies the value to find |
replace | Required. Specifies the value to replace the value in find |
string | Required. Specifies the string to be searched |
count | Optional. A variable that counts the number of replacements |
examples of the STR_REPLACE() function
Example 1. In this example, we replace the characters “world” in the string “Hello world!” with “Peter”.
<?php
echo str_replace("world","Peter","Hello world!");
?>
Example 2. In this example, we use str_replace() with an array and a count variable.
<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "Replacements: $i";
?>
Example 3. In this example, we use str_replace() with fewer elements in replace than find.
<?php
$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr));
?>