首页 > 开发 > PHP > 正文

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

2024-05-04 21:56:42
字体:
来源:转载
供稿:网友

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. ?> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表