PHP 弄乱了 HTML 字符集编码 [英] PHP messing with HTML Charset Encoding

查看:22
本文介绍了PHP 弄乱了 HTML 字符集编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常奇怪的问题.我有一个包含一些德语字母的网站,当它只有 html 而没有 php 时,符号是显示编码的属性,当我将其更改为 UTF-8 时,它们不显示,而不是 Ö 我得到 .当我将 html 放入 php 并使用 charset=iso-8859-1 编码在 Wamp 上使用 Zend studio 启动它时,我得到 � 而不是 Ö(我想补充一点,同样的 Ö 是单选按钮的值).当它在

标签中时,它会正确显示.你能告诉我如何解决这个问题.我查看了其他站点,它们使用 UTF-8 编码并正确显示相同的符号.我试图更改 php edior 编码,但我想这并不重要 -> Zend Studio 的编辑器中的所有内容都正确显示...提前致谢.

I have this very strange problem. I have a site that contains some German letters and when it's only html without php the symbols are property displayed with encoding when i change it to UTF-8 they dont display and instead of Ö I get �. When I put the html inside php and start it with Zend studio on Wamp with the charset=iso-8859-1 encoding I get � instead of Ö ( I want to add that this same Ö is a value of a radio button). When it's in a

tag it displays properly. Can you tell me how to fix this issue. I look at other sites and they have UTF-8 Encoding and displaying properly the same symbol. I tried to change the php edior encoding but it doesn't matter I suppose -> everything is displaying properly inside Zend Studio's editor... Thank you in advance.

推荐答案

您可能已经开始混合编码类型.例如.以 iso-8859-1 格式发送但从 MySQL 或 XML 获取 UTF-8 文本编码的页面通常会失败.

You have probably come to mix encoding types. For example. A page that is sent as iso-8859-1, but get UTF-8 text encoding from MySQL or XML would typically fail.

要解决此问题,您必须控制与您选择使用的内部编码类型相关的输入编码类型.

To solve this problem you must keep control on input ecodings type in relation to the type of encoding you have chosen to use internal.

如果您将其作为 iso-8859-1 发送,则用户的输入也是 iso-8859-1.

If you send it as an iso-8859-1, your input from the user is also iso-8859-1.

header("Content-type:text/html; charset: iso-8859-1");

如果 mysql 发送 latin1,你什么都不用做.

And if mysql sends latin1 you do not have to do anything.

但是,如果您的输入不是 iso-8859-1,则必须在将其发送给用户之前对其进行转换,或者在存储之前将其适配到 Mysql.

But if your input is not iso-8859-1 you must converted it, before it's sending to the user or to adapt it to Mysql before it's store.

mb_convert_encoding($text, mb_internal_encoding(), 'UTF-8'); // If it's UTF-8 to internal encoding

简而言之,您必须始终将输入转换为适合内部编码和转换器输出以匹配外部编码.

Short it means that you must always have input converted to fit internal encoding and convereter output to match the external encoding.

这是我选择使用的内部编码.

This is the internal encoding I have chosen to use.

mb_internal_encoding('iso-8859-1'); // Internal encoding

这是我使用的代码.

mb_language('uni'); // Mail encoding
mb_internal_encoding('iso-8859-1'); // Internal encoding
mb_http_output('pass'); // Skip

function convert_encoding($text, $from_code='', $to_code='')
{
    if (empty($from_code))
    {
        $from_code = mb_detect_encoding($text, 'auto');
        if ($from_code == 'ASCII')
        {
            $from_code = 'iso-8859-1';
        }
    }

    if (empty($to_code))
    {
        return mb_convert_encoding($text, mb_internal_encoding(), $from_code);
    }
    return mb_convert_encoding($text, $to_code, $from_code);
}

function encoding_html($text, $code='')
{
    if (empty($code))
    {
        return htmlentities($text, ENT_NOQUOTES, mb_internal_encoding());
    }

    return mb_convert_encoding(htmlentities($text, ENT_NOQUOTES, $code), mb_internal_encoding(), $code);
}
function decoding_html($text, $code='')
{
    if (empty($code))
    {
        return html_entity_decode($text, ENT_NOQUOTES, mb_internal_encoding());
    }

    return mb_convert_encoding(html_entity_decode($text, ENT_NOQUOTES, $code), mb_internal_encoding(), $code);
}

这篇关于PHP 弄乱了 HTML 字符集编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆