note 111176 added to function.strtr
| From: | qeremy | Date: | Tue, 22 Jan 2013 19:19:24 +0000 |
| Subject: | note 111176 added to function.strtr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-193349@lists.php.net to get a copy of this message | ||
Weird, but strtr corrupting chars, if used like below and if file is encoded in UTF-8;
<?php
$str = 'Ãbc Ãbc';
echo strtr($str, 'Ã', 'a');
// output: a�bc a�bc
?>
And a simple solution;
<?php
function strtr_unicode($str, $a = null, $b = null) {
$translate = $a;
if (!is_array($a) && !is_array($b)) {
$a = (array) $a;
$b = (array) $b;
$translate = array_combine(
array_values($a),
array_values($b)
);
}
// again weird, but accepts an array in this case
return strtr($str, $translate);
}
$str = 'Ãbc Ãbc';
echo strtr($str, 'Ã', 'a') ."\n";
echo strtr_unicode($str, 'Ã', 'a') ."\n";
echo strtr_unicode($str, array('Ã' => 'a')) ."\n";
// outputs
// a�bc a�bc
// abc abc
// abc abc
?>
----
Server IP: 95.130.168.26
Probable Submitter: 88.253.76.215
----
Manual Page -- http://php.net/manual/en/function.strtr.php
Edit -- https://master.php.net/note/edit/111176
Del: integrated -- https://master.php.net/note/delete/111176/integrated
Del: useless -- https://master.php.net/note/delete/111176/useless
Del: bad code -- https://master.php.net/note/delete/111176/bad+code
Del: spam -- https://master.php.net/note/delete/111176/spam
Del: non-english -- https://master.php.net/note/delete/111176/non-english
Del: in docs -- https://master.php.net/note/delete/111176/in+docs
Del: other reasons-- https://master.php.net/note/delete/111176
Reject -- https://master.php.net/note/reject/111176
Search -- https://master.php.net/manage/user-notes.php