2011-07-21 12:39
浏览 87

字符编码问题 - 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)

$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

图片转代码服务由CSDN问答提供 功能建议


  // $ booktitle =“Comíhabitaciónbailé”
 $ xml_obj = new DOMDocument('1.0','utf-8'); 
 //用booktitle创建的节点 添加到xml_obj 
 //没有htmlentities /其他转换完成
 $ returnHeader = drupal_http_request($ url,$ headers = array(“Content-Type:text / xml; charset = utf-8”),$ method =  'POST',$ data = $ xml_data,$ retry = 3); 

当我在最后收到它时(通过drupal_http_request)我做了很多事情 它,我得到以下内容:

  Com&amp; Atilde;&amp; shy;  habitaci&amp; Atilde;&amp; sup3; n bail&amp; Atilde;&amp; copy; 

显示时看起来像乱码: < pre> ComÃHabitaciónBailé


编辑 1)

 $ title =“Comíhabitaciónbailé”; 
echo“title = $ title 
echo'encoding is'.mb_detect_encoding  ($ title); 
 $ heutf8 = htmlentities($ title,ENT_COMPAT,“UTF-8”); 
echo“heutf8 = $ heutf8 


  title =Comíhabitaciónbailé
encoding是UTF-8heutf8 = 


  title =Comíhabitaciónbailé
encoding是UTF-8PHP警告:htmlentities  ():第5行/home/testaccount/public_html/test2.php中参数中的无效多字节序列
heutf8 = 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dops57958 2011-07-21 13:01

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

    解决 无用
    打赏 举报
  • doukeyong3746487 2011-07-21 12:42

    I think you shouldn't encode the entities with htmlentities just for outputting it correctly (you should as stated in the comments use htmlspecialchars to avoid cross side scripting) , just set the correct headers and meta end echo the values normally:

     header ('Content-type: text/html; charset=utf-8');
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    解决 无用
    打赏 举报
  • doutizha7526 2011-07-21 12:53

    Try passing headers information in a key/value array format.

    Something like

    $headers = array("Content-Type" => "text/xml; charset=utf-8"")

    解决 无用
    打赏 举报

相关推荐 更多相似问题