在php中将法语字符转换为字符串

时间:2012-04-09 07:35:57

标签: php

这里是我在文本框中输入的数据。文本框名称:quiz_optionA

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

正常的英文字母转换为字符串。但法语字符没有转换为字符串。

输出应为EEEabcd。你能帮帮我吗?

2 个答案:

答案 0 :(得分:0)

今天我已经在similar question得到了答复 所以尝试使用这样的HTML代码:

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

确保包含$ normalizeChars的.php文件具有utf8编码。

答案 1 :(得分:0)

你的行

echo strtr($string, $normalizeChars);

只会转换您在$normalizeChars中指定的字符。您想要翻译的内容,即É(注意:您在问题中未定义该字符的编码),$normalizeChars中没有任何翻译信息。

如果您希望翻译这些字符,则需要将它们添加到$normalizeChars数组中。看起来É实际上是A�(如果您添加hexdump,我们可以更好地说明这是什么)。

我假设以下内容:

浏览器以UTF-8编码将输入发送到您的应用程序。您可以用一些单字节编码(非utf-8)处理它们,这就是它不会改变的原因。

修改

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

这是在PHP字符串中编码的UTF-8:"\xC3\x89"。要将几乎任何字符编码为UTF-8,首先需要在编码中找到您的字符,它是unicode代码点。举个例子:

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

可以使用小型PHP函数将代码点转换为UTF-8:

/**
 * @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文件编码的影响。