doule6314 2011-07-21 04:39
浏览 92
已采纳

字符编码问题 - UTF-8 /在互联网上传输数据时出现问题?

I've got data being sent from a client side which is sending it like this:

// $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);

When I receive it at my end (via that drupal_http_request) and I do htmlentities on it, I get the following:

 Comí habitación bailé

Which when displayed looks like gibberish:

 Comí Habitación Bailé

What is going wrong?


Edit 1)

<?php
$title = "Comí habitación bailé";
echo "title=$title
";
echo 'encoding is '.mb_detect_encoding($title);
$heutf8 = htmlentities($title, ENT_COMPAT, "UTF-8");
echo "heutf8=$heutf8
";
?>

Running this test script on a Windows machine and redirecting to a file shows:

title=Comí habitación bailé
encoding is UTF-8heutf8=

Running this on a linux system:

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=

展开全部

  • 写回答

3条回答 默认 最新

  • dops57958 2011-07-21 05:01
    关注

    htmlentities interprets its input as ISO-8859-1 by default; are you passing UTF-8 for the charset parameter?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部