栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PHP将HTML Charset编码弄乱了

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP将HTML Charset编码弄乱了

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

要解决此问题,您必须相对于您选择使用内部编码的类型来保持对输入编码类型的控制。

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

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

如果mysql发送latin1,则您无需执行任何操作。

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

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

简短地说,这意味着您必须始终将输入转换为适合内部编码,并转换输出以匹配外部编码。


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

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

这是我使用的代码。

mb_language('uni'); // Mail encodingmb_internal_encoding('iso-8859-1'); // Internal encodingmb_http_output('pass'); // Skipfunction convert_encoding($text, $from_pre='', $to_pre=''){    if (empty($from_pre))    {        $from_pre = mb_detect_encoding($text, 'auto');        if ($from_pre == 'ASCII')        { $from_pre = 'iso-8859-1';        }    }    if (empty($to_pre))    {        return mb_convert_encoding($text, mb_internal_encoding(), $from_pre);    }    return mb_convert_encoding($text, $to_pre, $from_pre);}function encoding_html($text, $pre=''){    if (empty($pre))    {        return htmlentities($text, ENT_NOQUOTES, mb_internal_encoding());    }    return mb_convert_encoding(htmlentities($text, ENT_NOQUOTES, $pre), mb_internal_encoding(), $pre);}function decoding_html($text, $pre=''){    if (empty($pre))    {        return html_entity_depre($text, ENT_NOQUOTES, mb_internal_encoding());    }    return mb_convert_encoding(html_entity_depre($text, ENT_NOQUOTES, $pre), mb_internal_encoding(), $pre);}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/443193.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号