PHP strtr根本不起作用

时间:2012-03-14 23:11:47

标签: php translation strtr

即使我输入

echo strtr("-äåö-", "äåö", "xxx");

它无法正常输出此>xxx¥x¶<,但是当我使用下面的示例时,它根本不会翻译它保留原始的mambo jumbo。 如果我输入表单ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑŐŰÜÒÓÔÕÖØÝߟàáâãäåçèéêëìíîïñòóôõőöøšűùúûüýÿž并点击翻译,则会突然显示相同的字符串,æ œ根本不会翻译。

<form method="POST">
    <input style="width:500px;" type="text" name="first_name" />
    <input style="width:500px;" type="text" name="last_name" />
    <input type="submit" name="submit" value="translate" />
</form>


<?php

    $dict = array(
                    "Æ" => "AE", 
                    "æ" => "ae", 
                    "Œ" => "OE",
                    "œ" => "oe"
                 );

    $first = strtr($_POST['first_name'], $dict);         
    $last  = strtr($_POST['last_name'], $dict);      


    $first = strtr($first, 
                          "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑŐŰÜÒÓÔÕÖØÝߟàáâãäåçèéêëìíîïñòóôõőöøšűùúûüýÿž",
                          "AAAAAACEEEEIIIINOUUOOOOOOYSYaaaaaaceeeeiiiinooooooosuuuuuyyz"); 

    $last  = strtr($last, 
                          "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑŐŰÜÒÓÔÕÖØÝߟàáâãäåçèéêëìíîïñòóôõőöøšűùúûüýÿž",
                          "AAAAAACEEEEIIIINOUUOOOOOOYSYaaaaaaceeeeiiiinooooooosuuuuuyyz"); 

    echo $first." --- ";
    echo $last;
?>

即使我在代码顶部添加

foreach ($_POST as $key => $value) {
    $POST[$key] = iconv(mb_detect_encoding($_POST["first_name"]), "ASCII//TRANSLIT", $POST[$value]);
}

并粘贴AAAAAACEEEEIIIINOUUOOOOOOYSYaaaaaaceeeeiiiinooooooosuuuuuyyz它就像yAyAyAyEyEyIyIyNyUyOyOyOyYyYyayauaueyeyiyiynyoyoyoysyuuuyyyzy�y�y�y�y�y�y�y�y�y�y�y�y�y�y�y�y�y�uay�yuuzu�y�y�y�y�y�y�u�

一样

没关系,因为没有人知道它为什么不起作用我只是非常成功地使用str_replacestr_ireplace而无需担心编码。

编辑:我的错误编码对str_replace也很重要。我在html页面上使用

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

3 个答案:

答案 0 :(得分:3)

带有函数原型的

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));
}

答案 1 :(得分:1)

您是否尝试过mb_strstr:http://php.net/manual/en/function.mb-strstr.php

此功能支持多字节字符编码。

答案 2 :(得分:1)

听起来你可能有竞争编码。如果您的浏览器正在提交UTF8,但您的文件保存在(例如)8859-1中,则您的字符将不匹配,翻译将失败。另外,查看the doc page,有几条评论建议首先在输入字符串上使用utf8_decode()utf8_decode()本身可以做你想做的事。

UTF8是一种多字节编码(实际上,它是一种可变字节编码)。诸如÷ï之类的字符具有超过256的Unicode代码点,需要将其编码为两个或更多字节,均高于128,以识别字符。我怀疑你将不得不更多地了解Unicode。 utf8_encode还有另一种解释。

编辑:自从我与编码搏斗以来已经有一段时间了。您应该查看iconv()以获得更多通用的重新编码。