php5.4以上版本GBK編碼下htmlspecialchars輸出為空問題解決方法匯總
從舊版升級到php5.4,恐怕最麻煩的就是htmlspecialchars這個問題了!當然,htmlentities也會受影響,不過,對于中文站來說一般用htmlspecialchars比較常見,htmlentities非常少用到。
可能老外認為網(wǎng)頁普遍應(yīng)該是utf-8編碼的,于是苦了那些用GB2312,GBK編碼的中文站......!
具體表現(xiàn):
$str = "9enjoy.com的php版本是5.2.10";
echo htmlspecialchars($str);
gbk字符集下輸出為空...utf-8下,輸出正常。
為什么呢,原因在于5.4.0對這個函數(shù)的變化:
5.4.0 The default value for the encoding parameter was changed to UTF-8.
原來是什么呢?
string htmlspecialchars ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = 'UTF-8' [, bool $double_encode = true ]]] )
Defines encoding used in conversion. If omitted, the default value for this argument is ISO-8859-1 in versions of PHP prior to 5.4.0, and UTF-8 from PHP 5.4.0 onwards.
原來是ISO-8859-1,5.4后默認變成utf-8!然后中文使用這個函數(shù)就輸出為空白了。
國內(nèi)一堆開源程序在5.4下都會有這樣的問題,DISCUZ官方也建議用戶不要升級到5.4
解決方案:
1.苦逼的修改所有用到htmlspecialchars地方的程序
1.1 其第二個$flags參數(shù),默認是ENT_COMPAT,因此改成
htmlspecialchars($str,ENT_COMPAT,'GB2312');
為什么不是GBK?因為沒有GBK這個參數(shù),如果強行使用GBK,則報錯給你看:
Warning: htmlspecialchars(): charset `gbk' not supported, assuming utf-8
為了能使用GBK,則改成:
htmlspecialchars($str,ENT_COMPAT,'ISO-8859-1');
1.2.一樣是改程序,但可以省略一個參數(shù)。
可以在網(wǎng)頁頭部加
ini_set('default_charset','gbk');
然后改成
htmlspecialchars($str,ENT_COMPAT,'');
文檔中有寫:An empty string activates detection from script encoding (Zend multibyte), default_charset and current locale (see nl_langinfo() and setlocale()), in this order. Not recommended.
大概意思就是:傳入空字符串則使用default_charset的編碼
1.3.封裝一個函數(shù)吧...本來htmlspecialchars這個單詞一直不好記。
function htmlout($str) {
return htmlspecialchars($str,ENT_COMPAT,'ISO-8859-1');
}
然后去批量替換。
2.直接修改源碼,重編譯!這也是目前我在線上做的方案。
修改ext/standard/html.c
大概在372行
/* Default is now UTF-8 */
if (charset_hint == NULL)
return cs_utf_8;
把cs_utf_8改成 cs_8859_1
/* Default is now UTF-8 */
if (charset_hint == NULL)
return cs_8859_1;
編譯后,原程序就不用做任何調(diào)整了。
安裝方法可參考:http://www.dbjr.com.cn/article/63388.htm
windows下怎么辦?這個,自己想辦法編譯吧,難度比較大...
提供一個網(wǎng)址供參考:http://www.dbjr.com.cn/article/63391.htm
引用其一句話:準備好咖啡、可樂,做好準備,可能要折騰數(shù)小時…
- php htmlentities和htmlspecialchars 的區(qū)別
- php 去除html標記--strip_tags與htmlspecialchars的區(qū)別詳解
- PHP關(guān)于htmlspecialchars、strip_tags、addslashes的解釋
- php過濾輸入操作之htmlentities與htmlspecialchars用法分析
- php htmlspecialchars()與shtmlspecialchars()函數(shù)的深入分析
- php htmlspecialchars加強版
- PHP htmlspecialchars() 函數(shù)實例代碼及用法大全
- PHP htmlspecialchars_decode()函數(shù)用法講解
- PHP htmlspecialchars()函數(shù)用法與實例講解
- php過濾htmlspecialchars() 函數(shù)實現(xiàn)把預定義的字符轉(zhuǎn)換為 HTML 實體用法分析
相關(guān)文章
PHP不使用內(nèi)置函數(shù)實現(xiàn)字符串轉(zhuǎn)整型的方法示例
一般php字符串類型的數(shù)字如果想轉(zhuǎn)成整型的數(shù)字,我們都是采用系統(tǒng)內(nèi)置的API去做轉(zhuǎn)換,但下面這篇文章主要給大家介紹了關(guān)于PHP不使用內(nèi)置函數(shù)實現(xiàn)字符串轉(zhuǎn)整型的方法示例,文中介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07PHP 常用數(shù)組內(nèi)部函數(shù)(Array Functions)介紹
本章節(jié)我們還要學習一些其它常用的有關(guān)數(shù)組的內(nèi)部函數(shù):count,sizeof、sort、asort、ksort等等,感興趣的朋友可以參考下哈,希望對大家有所幫助2013-06-06淺析php插件 Simple HTML DOM 用DOM方式處理HTML
本篇文章是對php插件Simple HTML DOM 用DOM方式處理HTML進行了詳細的分析介紹,需要的朋友參考下2013-07-07