Ale*_*ski 1 php encoding utf-8 character-encoding
我有这个非常奇怪的问题。我有一个包含一些德语字母的网站,当它只是没有 php 的 html 时,符号是用编码显示的属性,当我将其更改为 UTF-8 时,它们不会显示,而不是 \xc3\x96 我得到 \xef\xbf\xbd。当我将 html 放入 php 并使用 Wamp 上的 Zend studio 使用 charset=iso-8859-1 编码启动它时,我得到 \xc3\xaf\xc2\xbf\xc2\xbd 而不是 \xc3\x96 (我想添加同样的 \xc3\x96 是单选按钮的值)。当它处于一个
标记它正确显示。你能告诉我如何解决这个问题吗?我查看其他网站,他们有 UTF-8 编码并正确显示相同的符号。我尝试更改 php edior 编码,但我想这并不重要 -> 一切都在 Zend Studio 的编辑器中正确显示...提前谢谢您。
\n您可能已经开始混合编码类型。例如。作为 iso-8859-1 发送但从 MySQL 或 XML 获取 UTF-8 文本编码的页面通常会失败。
要解决此问题,您必须控制与您选择使用内部编码类型相关的输入编码类型。
如果您将其作为 iso-8859-1 发送,则用户的输入也是 iso-8859-1。
header("Content-type:text/html; charset: iso-8859-1");
Run Code Online (Sandbox Code Playgroud)
如果 mysql 发送 latin1 你不需要做任何事情。
但是,如果您的输入不是 iso-8859-1,则必须在将其发送给用户之前对其进行转换,或者在存储之前将其适应 Mysql。
mb_convert_encoding($text, mb_internal_encoding(), 'UTF-8'); // If it's UTF-8 to internal encoding
Run Code Online (Sandbox Code Playgroud)
简而言之,这意味着您必须始终将输入转换为适合内部编码,并将转换器输出转换为与外部编码匹配。
这是我选择使用的内部编码。
mb_internal_encoding('iso-8859-1'); // Internal encoding
Run Code Online (Sandbox Code Playgroud)
这是我使用的代码。
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);
}
Run Code Online (Sandbox Code Playgroud)