strtr
с прототипом функции
string strtr ( string $str , string $from , string $to )
надежно работает только с однобайтовыми кодировками (например, ISO-8859-1).
header("Content-Type: text/plain; charset=ISO-8859-1");
$str = "\x2d\xe4\xe5\xf6\x2d"; // ISO-8859-1: -äåö-
$from = "\xe4\xe5\xf6"; // ISO-8859-1: äåö
$to = "\x78\x78\x78"; // ISO-8859-1: xxx
dump($str, "ISO-8859-1"); // length in octets: 5
dump($from, "ISO-8859-1"); // length in octets: 3
dump($to, "ISO-8859-1"); // length in octets: 3
print strtr($str, $from, $to); // -xxx-
Вывод:
-: 2d
ä: e4
å: e5
ö: f6
-: 2d
length (encoding: ISO-8859-1): 5
length in octets (8-bit-byte): 5
ä: e4
å: e5
ö: f6
length (encoding: ISO-8859-1): 3
length in octets (8-bit-byte): 3
x: 78
x: 78
x: 78
length (encoding: ISO-8859-1): 3
length in octets (8-bit-byte): 3
-xxx-
Если вы используете многобайтовые символы, например, из UTF-8, вы, вероятно, получите неверную строку:
header("Content-Type: text/plain; charset=UTF-8");
$str = "\x2d\xc3\xa4\xc3\xa5\xc3\xb6\x2d"; // UTF-8: -äåö-
$from = "\xc3\xa4\xc3\xa5\xc3\xb6"; // UTF-8: äåö
$to = "\x78\x78\x78"; // UTF-8: xxx
dump($str, "UTF-8"); // length in octets: 8
dump($from, "UTF-8"); // length in octets: 6
dump($to, "UTF-8"); // length in octets: 3
// > If from and to have different lengths, the extra characters in the longer
// > of the two are ignored. The length of str will be the same as the return
// > value's.
// http://de.php.net/manual/en/function.strtr.php
// This means that the $from-string gets cropped to "\xc3\xa4\xc3" (16 bit of
// the first char [ä] and the first 8 bit of the second char [å]):
strtr($str, $from, $to) === strtr($str, "\xc3\xa4\xc3", $to); // true
print strtr($str, $from, $to); // -xxx�x�-
Вывод:
-: 2d
ä: c3a4
å: c3a5
ö: c3b6
-: 2d
length (encoding: UTF-8): 5
length in octets (8-bit-byte): 8
ä: c3a4
å: c3a5
ö: c3b6
length (encoding: UTF-8): 3
length in octets (8-bit-byte): 6
x: 78
x: 78
x: 78
length (encoding: UTF-8): 3
length in octets (8-bit-byte): 3
-xxx�x�-
Для многобайтовых кодировок, таких как UTF-8, вам необходимо использовать прототип второй функции:
string strtr ( string $str , array $replace_pairs )
header("Content-Type: text/plain");
$str = "-äåö-"; // UTF-8 \x2d\xc3\xa4\xc3\xa5\xc3\xb6\x2d
$replace_pairs = array(
"ä" /* UTF-8 \xc3\xa4 */ => "x",
"å" /* UTF-8 \xc3\xa5 */ => "x",
"ö" /* UTF-8 \xc3\xb6 */ => "x"
);
print strtr($str, $replace_pairs); // -xxx-
Если кодировки не соответствуют вамнужно преобразовать их с помощью iconv :
header("Content-Type: text/plain");
$str = "\x2d\xe4\xe5\xf6\x2d"; // ISO-8859-1 -äåö-
$str = iconv("ISO-8859-1", "UTF-8", $str);
$replace_pairs = array(
"ä" /* UTF-8 \xc3\xa4 */ => "x",
"å" /* UTF-8 \xc3\xa5 */ => "x",
"ö" /* UTF-8 \xc3\xb6 */ => "x"
);
print strtr($str, $replace_pairs); // -xxx-
дамп функции:
// outputs the hexvalue for each char for the given encoding
function dump($data, $encoding) {
for($i = 0, $len = iconv_strlen($data, $encoding); $i < $len; ++$i) {
$char = iconv_substr($data, $i, 1, $encoding);
printf("%s: %s\n", $char, bin2hex($char));
}
printf("length (encoding: %s): %d\n", $encoding, $len);
printf("length in octets (8-bit-byte): %d\n\n", strlen($data));
}