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


convert french character in to string in 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。你能帮我做这件事吗?

今天我得到了类似的问题的答案所以尝试像这样使用 html 代码:

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

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

你的行

echo strtr($string, $normalizeChars);

将仅转换您在 $normalizeChars 中指定的字符。您错过要翻译的那些,即É(注意:您在问题中未定义该字符的编码),$normalizeChars中没有任何翻译信息。

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

我假设如下:

浏览器以 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文件的编码的影响。