PHP 8.5.0 Alpha 4 available for testing

Voting

: five plus two?
(Example: nine)

The Note You're Voting On

Hayley Watson
12 years ago
Since strtr (like PHP's other string functions) treats strings as a sequence of bytes, and since UTF-8 and other multibyte encodings use - by definition - more than one byte for at least some characters, the three-string form is likely to have problems. Use the associative array form to specify the mapping.

<?php
// Assuming UTF-8
$str = 'Äbc Äbc'; // strtr() sees this as nine bytes (including two for each Ä)
echo strtr($str, 'Ä', 'a'); // The second argument is equivalent to the string "\xc3\x84" so "\xc3" gets replaced by "a" and the "\x84" is ignored

echo strtr($str, array('Ä' => 'a')); // Works much better
?>

<< Back to user notes page

To Top