php htmlentities漢字中文亂碼問題解決辦法
來源:程序員人生 發布時間:2014-04-20 20:44:22 閱讀次數:4992次
htmlentities函數作用在漢字變量中的時候會出現亂碼,代碼如下:$resultsText = str_replace("[QUERY]", htmlentities($query), $resultsText);
正確的做法是改變htmlentities的默認參數:htmlentities($query,ENT_COMPAT,'UTF-8'),代碼如下:
- <?php
- $query='你好';
- $resultsText='1 條與 "[QUERY]" 相關的搜索結果';
- $resultsText = str_replace("[QUERY]", htmlentities($query,ENT_COMPAT,'UTF-8'), $resultsText);
- header('content-type: text/html; charset=utf-8');
-
- print_r($resultsText);
- ?>
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈