PHPverse 2025

Voting

: nine minus four?
(Example: nine)

The Note You're Voting On

qeremy [atta] gmail [dotta] com
12 years ago
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
?>

<< Back to user notes page

To Top