Menu Home

strtr

osbo.com | PHP | Functions | strtr

Translate characters or replace substrings

Syntax

strtr ( string $str , string $from , string $to ) : string
strtr ( string $str , array $replace_pairs ) : string

Parameters

str

The string being translated.

from

The string being translated to to.

to

The string replacing from.

replace_pairs

The replace_pairs parameter may be used instead of to and from, in which case it's an array in the form array('from' => 'to', ...).

Return

Returns the translated string. If replace_pairs contains a key which is an empty string (""), FALSE will be returned. If the str is not a scalar then it is not typecasted into a string, instead a warning is raised and NULL is returned.

Examples

str from to

<?

$str = "abcde edcba";
$from = "abc";
$to = "123";

$return = strtr($str, $from, $to);

echo $return;

?>
123de ed321

str replace_pairs

<?

$str = "abcde edcba";
$replace_pairs = array("abc" => "123", "cba" => "321");

$return = strtr($str, $replace_pairs);

echo $return;

?>
123de ed321

PHP