note 111270 added to function.strtr
| From: | HayleyWatson | Date: | Fri, 01 Feb 2013 02:04:56 +0000 |
| Subject: | note 111270 added to function.strtr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-193473@lists.php.net to get a copy of this message | ||
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
?>
----
Server IP: 69.147.83.199
Probable Submitter: 122.57.198.170
----
Manual Page -- http://php.net/manual/en/function.strtr.php
Edit -- https://master.php.net/note/edit/111270
Del: integrated -- https://master.php.net/note/delete/111270/integrated
Del: useless -- https://master.php.net/note/delete/111270/useless
Del: bad code -- https://master.php.net/note/delete/111270/bad+code
Del: spam -- https://master.php.net/note/delete/111270/spam
Del: non-english -- https://master.php.net/note/delete/111270/non-english
Del: in docs -- https://master.php.net/note/delete/111270/in+docs
Del: other reasons-- https://master.php.net/note/delete/111270
Reject -- https://master.php.net/note/reject/111270
Search -- https://master.php.net/manage/user-notes.php