【问题标题】:convert french character in to string in php在php中将法语字符转换为字符串
【发布时间】:2012-04-09 07:35:57
【问题描述】:

这里是我在表单文本框中输入的数据。文本框名称: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。你能帮我做这件事吗?

【问题讨论】:

  • 您的 PHP 文件是否以浏览器显示的相同编码保存?您是否有任何标头指定编码?
  • 你必须使用多字节字符串函数stackoverflow.com/questions/9986584/…
  • 我的编辑器使用“cp1252”字符编码。它显示“某些字符无法使用“cp1252”字符编码映射。要么更改编码,要么删除“cp1252”不支持的字符“字符编码”..如果我保存为 utf8,它工作得很好。在 php 彻底编码中将字符转换为 utf8 格式的任何其他方式?

标签: php


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    你的线路

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

    【讨论】:

    • 我的编辑器使用“cp1252”字符编码。它显示“某些字符无法使用“cp1252”字符编码映射。要么更改编码,要么删除“cp1252”不支持的字符“字符编码”..如果我保存为 utf8,它工作得很好。在 php 彻底编码中将字符转换为 utf8 格式的任何其他方式?
    • @ram:我扩展了答案。为了验证某些东西是否是有效的 UTF-8,我交叉链接了一个问题:Fast way to strip all characters not displayable in browser from utf8 string(您可能不需要这个)。
    猜你喜欢
    • 2014-05-20
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2013-05-12
    • 2011-10-16
    • 2015-02-05
    相关资源
    最近更新 更多