The str_replace()
string function can be used to replace all occurrences of one or more characters with none, one or more characters. A count parameter to count replacements is optional.
Syntax
With optional count parameter:
$result = str_replace($before, $after, $target, $count);
Without optional count parameter:
$result = str_replace($before, $after, $target);
$result
The returned string or array with replaced character(s). The str_replace()
string function will return the same data type as applied to $target
. See examples below.
$before (search parameter)
Required parameter as string data type or array. Specifies the characters to be found in $target
and to be replaced.
$after (replace parameter)
Required parameter as string data type or array. Specifies the characters to be used in place of the found characters in $target
.
$target
Required parameter as string data type or array. Specifies the string or array to be searched for character(s) as specified in $before
and replaced with character(s) as specified in $after
.
$count (count parameter)
Optional parameter as integer data type. Variable that contains the number of character replacements. See examples below.
Examples
Code snippets in examples below have been tested in PHP parser version 5.3.27.
Character Replacements in String - $before
and $after
as String
Examples show how character are replaced in a string while using the search ($before
) and replace ($after
) parameter as string data type.
Without optional count parameter:
$result = str_replace($before, $after, $target);
$target | $before | $after | $result |
---|---|---|---|
"Rainy weather is dry weather." | "dry" | "wet" | "Rainy weather is wet weather." |
"Remove all spaces." | " " | "" | "Removeallspaces." |
"Open 7 days a week." | "7" | "seven" | "Open seven days a week." |
"Open 7 days a week." | 71) | "seven" | "Open seven days a week." |
"Open seven days a week." | "seven" | "7" | "Open 7 days a week." |
"Open seven days a week." | "seven" | 71) | "Open 7 days a week." |
"Open seven days a week." | "SEVEN" | "7" | "Open seven days a week."2) |
1) Integer values are not defined but work in this example.
2) The str_replace
function is case sensitive and no characters were replaced.
With optional count parameter:
$result = str_replace($before, $after, $target, $count);
echo $count;
$target | $before | $after | $result |
---|---|---|---|
"I prefer green apples." | "green" | "red" | "I prefer red apples." |
"All green apples in green box." | "green" | "red" | "All red apples in red box." |
"Green apples in green box." | "green" | "red" | "Green apples in red box." |
Character Replacements in String - $before
and $after
as Array
Examples show how character are replaced in a string while using the search ($before
) and replace ($after
) parameter as array data type.
Without optional count parameter:
$before = array();
$after = array();
$result = str_replace($before, $after, $target);
$target | $before | $after | $result |
---|---|---|---|
"7 days a week." | $before[0] = "week" | $after[0] = "day" | "24 hours a day." |
With optional count parameter:
$before = array();
$after = array();
$result = str_replace($before, $after, $target, $count);
echo $count;
$target | $before | $after | $result |
---|---|---|---|
"7 days a week." | $before[0] = "7" | $after[0] = "24" | "24 hours a day." |
"7 days a week." | $before[0] = "7" | $after[0] = "24" | "24 hours a hour." |
"Prefer green apples." | $before[0] = "green" | $after[0] = "red" | "Prefer red ." |
1) Note that this time 4 replacements were counted, week
was replaced with day
as specified in array element [1]
and replaced again with hour
as specified in array element [2]
.
2) The $after
array has fever elements than the $before
array.
Character Replacements in Array - $before
and $after
as String
Examples show how character are replaced in an array while using the search ($before
) and replace ($after
) parameter as string data type.
With optional count parameter:
$target = array();
$result = str_replace($before, $after, $target, $count);
echo $count;
$target | $before | $after | $result |
---|---|---|---|
$target[0] = "Green apples." | "Green" | "Red" | $result[0] = "Red apples." |
$target[0] = "Green apples." | "green" | "red" | $result[0] = "Green apples." |
Character Replacements in Array - $before
and $after
as Array
Above examples can be combined as required. Character replacements in arrays while using the search ($before
) and replace ($after
) parameter as array data types are identical to the character replacements in arrays while using single string data types.