即使我打字的时候
echo strtr("-äåö-", "äåö", "xxx");它不能正常工作,它输出这个>xxx¥x¶<,但是当我使用下面的例子时,它根本没有翻译任何东西,它保持了原始的mambo jumbo。如果我键入表单ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑŐŰÜÒÓÔÕÖØÝߟàáâãäåçèéêëìíîïñòóôõőöøšűùúûüýÿž并单击translate,它会输出相同的字符串,而æ œ根本不会翻译。
<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_replace和str_ireplace,而不需要考虑任何编码问题。
编辑:我糟糕的编码对str_replace来说也很重要。我在html页面上使用过
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />发布于 2012-03-19 05:38:09
带有函数原型的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));
}发布于 2012-03-15 07:19:08
你试过mb_strstr了吗:http://php.net/manual/en/function.mb-strstr.php
此函数支持多字节字符编码。
发布于 2012-03-15 10:02:55
听起来你可能有相互竞争的编码。如果您的浏览器正在提交UTF8,但您的文件保存在(例如) 8859-1中,则您的字符将不匹配,翻译将失败。此外,看看the doc page,有几条评论建议首先在输入字符串上使用utf8_decode()。utf8_decode()本身可能会做您想要做的事情。
UTF8是一种多字节编码(实际上,它是一种可变字节编码)。诸如÷或ï之类的字符的Unicode码位超过256,需要将其编码成两个或更多字节,这两个字节都在128以上,用于标识字符。我怀疑您将不得不学习更多关于Unicode的知识。在utf8_encode有另一种解释。
编辑:我已经有一段时间没有纠结于编码了。您应该看看iconv(),以获得更通用的重新编码。
https://stackoverflow.com/questions/9711735
复制相似问题