Home  >  Article  >  php教程  >  php htmlentities汉字中文乱码问题解决办法

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

WBOY
WBOYOriginal
2016-05-25 16:51:531759browse

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

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

<?php  
$query=&#39;你好&#39;;  
$resultsText=&#39;1 条与 "[QUERY]" 相关的搜索结果&#39;;  
$resultsText = str_replace("[QUERY]", htmlentities($query,ENT_COMPAT,&#39;UTF-8&#39;), $resultsText);  
header(&#39;content-type: text/html; charset=utf-8&#39;);  

print_r($resultsText);

               
               

文章网址:

随意转载^^但请附上教程地址。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn