Member Login:


PHP: Replace Characters/Part Of A String

Document Reference: TN200901006 - Rev: 4.19 - Last Update: 31-03-2014 13:54 GMT - Downloaded: 19-Mar-2024 04:52 GMT

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."
$count => 1
"All green apples in green box.""green""red""All red apples in red box."
$count => 2
"Green apples in green box.""green""red""Green apples in red box."
$count => 1

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"
$before[1] = "7"
$before[2] = "days"
$after[0] = "day"
$after[1] = "24"
$after[2] = "hours"
"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"
$before[1] = "day"
$before[2] = "week"
$after[0] = "24"
$after[1] = "hour"
$after[2] = "day"
"24 hours a day."
$count => 3
"7 days a week."$before[0] = "7"
$before[1] = "week"
$before[2] = "day"
$after[0] = "24"
$after[1] = "day"
$after[2] = "hour"
"24 hours a hour."
$count => 41)
"Prefer green apples."$before[0] = "green"
$before[1] = "apples"
$after[0] = "red""Prefer red ."
$count => 22)

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."
$target[1] = "Green cherries."
$target[2] = "Green berries."
"Green""Red"$result[0] = "Red apples."
$result[1] = "Red cherries."
$result[2] = "Red berries."
$count => 3
$target[0] = "Green apples."
$target[1] = "Green cherries."
$target[2] = "Green berries."
"green""red"$result[0] = "Green apples."
$result[1] = "Green cherries."
$result[2] = "Green berries."
$count => 0

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.

This website uses cookies to give you the best experience on our website, to personalise content and to analyse our website traffic. Some cookies may have been set already. To find out more about our use of cookies you can visit our Privacy Statement. By browsing this website, you agree to our use of cookies.

Hide this message