PHP » Strings » strtr()

Syntax:
string strtr(string s, string from, string to)
string strtr(string s, array replace)
s
String to make replacements in.
from
Characters to replace.
to
Replacements for the characters.
s
String to make replacements in.
replace
Associative array with replacement strings.

Makes multiple replacements in a string.

There are two different ways of using this function. When used with three string parameters, this function can be used to replace single characters with other single characters. The from string contains the characters that should be replaced, and the to string lists their replacements A new string with the replacements made is returned.

strtr() can also be used to replace sequences of characters. In this case the from and to strings are replaced with a single associative array with the search terms as keys and the replacements as values.

Examples

Code:
<?php

$s = "Pizza: $12, Spaghetti: $6, Hamburger: £5";
print "$s<br>";
print strtr($s, "\$£", "£€");

?>
Output:
Pizza: $12, Spaghetti: $6, Hamburger: £5
Pizza: £12, Spaghetti: £6, Hamburger: €5
Explanation:

This code replaces dollar signs with pound signs, and pound signs with euro signs.

See Also: