php5.4以上版本GBK編碼下htmlspecialchars輸出為空問題解決方法匯總
從舊版升級到php5.4,恐怕最麻煩的就是htmlspecialchars這個(gè)問題了!當(dāng)然,htmlentities也會受影響,不過,對于中文站來說一般用htmlspecialchars比較常見,htmlentities非常少用到。
可能老外認(rèn)為網(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對這個(gè)函數(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后默認(rèn)變成utf-8!然后中文使用這個(gè)函數(shù)就輸出為空白了。
國內(nèi)一堆開源程序在5.4下都會有這樣的問題,DISCUZ官方也建議用戶不要升級到5.4
解決方案:
1.苦逼的修改所有用到htmlspecialchars地方的程序
1.1 其第二個(gè)$flags參數(shù),默認(rèn)是ENT_COMPAT,因此改成
htmlspecialchars($str,ENT_COMPAT,'GB2312');
為什么不是GBK?因?yàn)闆]有GBK這個(gè)參數(shù),如果強(qiáng)行使用GBK,則報(bào)錯(cuò)給你看:
Warning: htmlspecialchars(): charset `gbk' not supported, assuming utf-8
為了能使用GBK,則改成:
htmlspecialchars($str,ENT_COMPAT,'ISO-8859-1');
1.2.一樣是改程序,但可以省略一個(gè)參數(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.封裝一個(gè)函數(shù)吧...本來htmlspecialchars這個(gè)單詞一直不好記。
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.dhdzp.com/article/63388.htm
windows下怎么辦?這個(gè),自己想辦法編譯吧,難度比較大...
提供一個(gè)網(wǎng)址供參考:http://www.dhdzp.com/article/63391.htm
引用其一句話:準(zhǔn)備好咖啡、可樂,做好準(zhǔn)備,可能要折騰數(shù)小時(shí)…
- php htmlentities和htmlspecialchars 的區(qū)別
- php 去除html標(biāo)記--strip_tags與htmlspecialchars的區(qū)別詳解
- PHP關(guān)于htmlspecialchars、strip_tags、addslashes的解釋
- php過濾輸入操作之htmlentities與htmlspecialchars用法分析
- php htmlspecialchars()與shtmlspecialchars()函數(shù)的深入分析
- php htmlspecialchars加強(qiáng)版
- PHP htmlspecialchars() 函數(shù)實(shí)例代碼及用法大全
- PHP htmlspecialchars_decode()函數(shù)用法講解
- PHP htmlspecialchars()函數(shù)用法與實(shí)例講解
- php過濾htmlspecialchars() 函數(shù)實(shí)現(xiàn)把預(yù)定義的字符轉(zhuǎn)換為 HTML 實(shí)體用法分析
相關(guān)文章
Windows平臺PHP+IECapt實(shí)現(xiàn)網(wǎng)頁批量截圖并創(chuàng)建縮略圖功能詳解
這篇文章主要介紹了Windows平臺PHP+IECapt實(shí)現(xiàn)網(wǎng)頁批量截圖并創(chuàng)建縮略圖功能,結(jié)合實(shí)例形式分析了IECapt的下載、安裝及截圖、生成縮略圖相關(guān)使用技巧,需要的朋友可以參考下2019-08-08
php實(shí)現(xiàn)四舍五入的方法小結(jié)
這篇文章主要介紹了php實(shí)現(xiàn)四舍五入的方法,實(shí)例總結(jié)了php實(shí)現(xiàn)四舍五入的三種常用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
require(),include(),require_once()和include_once()區(qū)別
面試中最容易提到的一個(gè)PHP的問題,我想和大家共勉一下: require()和include()有許多相似之處,也有些不同。理解它們的不同點(diǎn)非常重要,否則很容易犯錯(cuò)誤。2008-03-03
php 的加密函數(shù) md5,crypt,base64_encode 等使用介紹
php 在做注冊、登錄或是url 傳遞參數(shù)時(shí)都會用到 字符變量的加密,下面我們就來簡單的介紹下:php 自帶的加密函數(shù)2012-04-04

