PHP乱七八糟的HTML字符集编码 [英] PHP messing with HTML Charset Encoding

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

问题描述

我有这个很奇怪的问题。我有一个网站包含一些德国字母,当它只有html没有php的符号是显示与编码的属性,当我改变为UTF-8他们不显示,而不是Ö我得到 。当我把html里面php和启动它与Zend工作室在Wamp与charset = iso-8859-1编码我得到�而不是Ö(我要添加,同样Ö是一个单选按钮的值) 。当它在

标记中时,它会正常显示。你能告诉我如何解决这个问题。我看看其他网站,他们有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.

要解决这个问题,

如果以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

这意味着你必须始终将输入转换为适合内部编码和convereter输出以匹配外部编码。

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

这是我选择使用的内部编码。 / p>

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天全站免登陆