Php 字符编码问题-在internet上传输数据时出现UTF-8/问题?
我从一个客户端发送数据,它是这样发送的:Php 字符编码问题-在internet上传输数据时出现UTF-8/问题?,php,drupal,utf-8,character-encoding,Php,Drupal,Utf 8,Character Encoding,我从一个客户端发送数据,它是这样发送的: // $booktitle = "Comí habitación bailé" $xml_obj = new DOMDocument('1.0', 'utf-8'); // node created with booktitle and added to xml_obj // NO htmlentities / other transformations done $returnHeader = drupal_http_request($url,
// $booktitle = "Comí habitación bailé"
$xml_obj = new DOMDocument('1.0', 'utf-8');
// node created with booktitle and added to xml_obj
// NO htmlentities / other transformations done
$returnHeader = drupal_http_request($url, $headers = array("Content-Type: text/xml; charset=utf-8"), $method = 'POST', $data = $xml_data, $retry = 3);
当我最终收到它(通过drupal_http_请求)并对其执行htmlentities时,我得到以下结果:
Comí habitación bailé
显示时看起来像胡言乱语:
Comà Habitación Bailé
出什么事了
编辑1) 在linux系统上运行此操作:
title=Comí habitación bailé
encoding is UTF-8PHP Warning: htmlentities(): Invalid multibyte sequence in argument in /home/testaccount/public_html/test2.php on line 5
heutf8=
我认为您不应该仅为了正确输出而使用htmlspecialchars对实体进行编码(您应该如评论中所述使用htmlspecialchars以避免跨端脚本编写),只需设置正确的头并正常地回显值:
<?php
header ('Content-type: text/html; charset=utf-8');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
</body>
</html>
尝试以键/值数组格式传递标题信息 差不多
$headers=array(“Content-Type”=>“text/xml;charset=utf-8”)
htmlentities
默认情况下将其输入解释为ISO-8859-1;是否为charset参数传递utf-8?看起来您得到的是ISO-8859而不是UTF8..!好的,这是有意义的,但是在这种情况下,drupal\u http\u请求()调用正在对我的代码发出API请求。我的代码将其按指示保存到MySQL数据库中…因此我们实际上还没有达到显示阶段…MySQL中的表是“latin1_swedish_ci”类型的(这是问题的原因吗?)坏主意,除非你喜欢你网站上的漏洞。至少当你使用不可信的数据时,至少使用<代码> HTMLPrimeCARS 。,您可以在页面上回显,而无需转义
。当然,在现实世界中,他们可能会做一些比仅仅警告“pwne!”!“。在客户端执行此操作-$headers=array(“内容类型:text/xml;charset=utf-8”)您的$headers
var与我的不同。@AlanBeats:您的问题在于$title
包含非utf-8字符(您的源文件可能是ISO-8859-1)。不幸的是,mb\u detect\u编码
似乎把它搞砸了,除非你将其“strict”参数传递为true,报告“UTF-8”“即使字符串实际上不是UTF-8。关于您关于htmlentities的原始观点,请参见编辑1-我在那里做的事情是否正确?@AlanBeats:您在那里做的事情是正确的,除了$title
中的数据不是UTF-8的部分。谢谢!这就是问题所在。。。!
title=Comí habitación bailé
encoding is UTF-8PHP Warning: htmlentities(): Invalid multibyte sequence in argument in /home/testaccount/public_html/test2.php on line 5
heutf8=
<?php
header ('Content-type: text/html; charset=utf-8');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
</body>
</html>