Scholar Repository
Home>Manual>Translate certain characters

Translate certain characters


(PHP 4, PHP 5)

strtrTranslate certain characters


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

This function returns a copy of str, translating all occurrences of each character in from to the corresponding character in to.

If from and to are different lengths, the extra characters in the longer of the two are ignored.



The string being translated.


The string being translated to to.


The string replacing from.


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

Return Values

Returns the translated string.

If replace_pairs contains a key which is an empty string (""), FALSE will be returned.


Example #1 strtr() example


strtr() may be called with only two arguments. If called with two arguments it behaves in a new way: from then has to be an array that contains string -> string pairs that will be replaced in the source string. strtr() will always look for the longest possible match first and will *NOT* try to replace stuff that it has already worked on.

Example #2 strtr() example with two arguments

= array("hello" => "hi""hi" => "hello");
strtr("hi all, I said hello"$trans);

The above example will output:

hello all, I said hi

See Also

  • str_replace() - Replace all occurrences of the search string with the replacement string
  • preg_replace() - Perform a regular expression search and replace

Home>Manual>Translate certain characters