国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > php htmlentities汉字中文乱码问题解决办法

php htmlentities汉字中文乱码问题解决办法

来源:程序员人生   发布时间:2014-04-20 20:44:22 阅读次数:3721次

htmlentities函数作用在汉字变量中的时候会出现乱码,代码如下:$resultsText = str_replace("[QUERY]", htmlentities($query), $resultsText);

正确的做法是改变htmlentities的默认参数:htmlentities($query,ENT_COMPAT,'UTF-8'),代码如下:

  1. <?php  
  2.     $query='你好';  
  3.     $resultsText='1 条与 "[QUERY]" 相关的搜索结果';  
  4.     $resultsText = str_replace("[QUERY]", htmlentities($query,ENT_COMPAT,'UTF-8'), $resultsText);  
  5.     header('content-type: text/html; charset=utf-8');  
  6.    
  7.     print_r($resultsText);  
  8. ?> 
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生