2012-04-09 81 views
1

这里的数据是我在窗体中的文本框中输入的。文本框名称:quiz_optionA将法语字符转换为php中的字符串

value = ÉÉÉabcd. 

我从我的PHP函数的数据如下方式

$this->_data = JRequest::get('post'); 
$string = $this->_data['quiz_optionA']; 

以下方法我用法语转换成英文

$normalizeChars = array(
'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A',  'Ã'=>'A', 'Ä'=>'A', 
'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 
'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 
'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 
'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 
'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 
'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f' 
); 


echo strtr($string, $normalizeChars);die; 

输出:

A�A�A�abcd 

Normal en glish字母转换为字符串。但法国字符没有转换成字符串。

输出应该是EEEabcd。你能帮我做这个吗?

+0

您的PHP文件是否以您的浏览器显示的相同编码保存?你有没有指定编码的标题? – Jon 2012-04-09 07:42:33

+0

你必须使用多字节字符串函数 http://stackoverflow.com/questions/9986584/dealing-with-non-ascii-string-as-array-and-character – max 2012-04-09 07:42:54

+0

我的编辑使用“cp1252”字符编码。它显示我“某些字符不能用”cp1252“字符编码映射。请更改编码或删除”cp1252“字符编码不支持的字符”。如果我保存为utf8,它工作正常。任何其他在PHP彻底编码中将字符转换为utf8格式的方法? – ram 2012-04-09 14:06:34

回答

0

今天我已经回答了similar question 所以尽量用html代码:

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

,并确保其包含$ normalizeChars你的PHP文件有utf8编码。

0

你行

echo strtr($string, $normalizeChars); 

仅转换您在$normalizeChars指定的字符。你想要翻译的那个,即É(注意:你在这个问题中没有定义那个字符的编码),没有任何翻译信息在$normalizeChars

如果您希望这些字符也可以翻译,您需要将它们添加到$normalizeChars阵列。它看起来像É实际上是A�(如果您添加一个hexdump,我们可以更好地说出这是什么)。

我会假设如下:

的浏览器将输入到应用程序中的UTF-8编码。你用一些单字节编码(non-utf-8)处理它们,这就是为什么它不会改变。

编辑:

É; cp1252 #201; LATIN CAPITAL LETTER E WITH ACUTE; U+00C9 

即UTF-8 PHP字符串内编码:"\xC3\x89"。要将几乎任何字符编码为UTF-8,首先需要在您的编码中找到您的字符,并且它是unicode codepoint。与您的例子:

Character: É 
Codepoint: LATIN CAPITAL LETTER E WITH ACUTE (U+00C9) 

的码点可以转换为UTF-8具有小的PHP函数:

/** 
* @see Unicode 6.0.0 Ch2 General Structure, rfc3629 
* @param int|string $codepoint e.g. 0xC9/"U+00C9" 
* @return string 
*/ 
function unicodeCodePointToUTF8($codepoint) 
{ 
    is_string($codepoint) && sscanf($codepoint, 'U+%x', $codepoint); 
    if ($codepoint < 0) { 
     throw new InvalidArgumentException('Lower than 0x00.'); 
    } 
    if ($codepoint > 0x10FFFD) { 
     throw new InvalidArgumentException('Larger than 0x10FFFD.'); 
    } 
    if (0xD800 <= $codepoint && $codepoint <= 0xDFFF) { 
     throw new InvalidArgumentException(sprintf('High and low surrogate halves are invalid unicode codepoints (U+D800 through U+DFFF, is U+%04X).', $codepoint)); 
    } 
    if ($codepoint <= 0x7F) { 
     return chr($codepoint); 
    } 
    if ($codepoint <= 0x7FF) { 
     return chr(0xC0 | $codepoint >> 6 & 0x1F) . chr(0x80 | $codepoint & 0x3F); 
    } 
    if ($codepoint <= 0xFFFF) { 
     return chr(0xE0 | $codepoint >> 12 & 0xF) . chr(0x80 | $codepoint >> 6 & 0x3F) . chr(0x80 | $codepoint & 0x3F); 
    } 
    return chr(0xF0 | $codepoint >> 18 & 0x7) . chr(0x80 | $codepoint >> 12 & 0x3F) . chr(0x80 | $codepoint >> 6 & 0x3F) . chr(0x80 | $codepoint & 0x3F); 
} 

用法:

echo bin2hex(unicodeCodePointToUTF8(0x00C9)), "\n"; # c389 

十六进制输出可以在串被写入在PHP中用前缀为\x的双引号字符串形式表示:

$binary = "\xC3\x89"; 

这种写作方式不受实际PHP文件编码的影响。

+0

我的编辑器以“cp1252”字符编码工作,它显示“某些字符不能使用”cp1252“字符编码映射。请更改编码或删除”cp1252“字符编码不支持的字符。我保存为utf8,它工作得很好。任何其他方式转换字符作为UTF8格式在PHP彻底编码? – ram 2012-04-09 14:06:57

+0

@ram:我扩展了答案。为了验证UTF-8是否有效,我将一个问题交叉链接起来:[快速去除不能在浏览器中显示的所有字符从utf8字符串](http://stackoverflow.com/a/7635283/367456)(您可能不会需要这个)。 – hakre 2012-04-10 14:24:19