This article mainly introduces how to solve the problem of empty output in htmlspecialchars under GBK encoding of php5.4 and later versions. This article provides a variety of solutions to this problem, if you want to upgrade from the old version to php5.4, I am afraid the most troublesome problem is htmlspecialchars! Of course, htmlentities will also be affected. However, htmlspecialchars is common for Chinese websites and htmlentities are rarely used.
Foreigners may think that web pages are generally UTF-8 encoded, so they suffer from Chinese websites that use GB2312 and GBK encoding ......!
Specific performance:
The code is as follows:
$ Str = "9enjoy.com's php version is 5.2.10 ";
Echo htmlspecialchars ($ str );
The output in the gbk character set is null... UTF-8, and the output is normal.
Why? the reason is the change to this function in 5.4.0:
The code is as follows:
5.4.0 The default value for the encoding parameter was changed to UTF-8.
What was it?
The code is as follows:
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.
The original is ISO-8859-1, 5.4 after the default into UTF-8! Then use this function in Chinese and the output is blank.
A bunch of open-source programs in China will have such problems in 5.4. DISCUZ officially recommends that users do not upgrade to 5.4.
Solution:
1. modify all programs that use htmlspecialchars.
1.1 The second $ flags parameter. the default value is ENT_COMPAT.
The code is as follows:
Htmlspecialchars ($ str, ENT_COMPAT, 'gb2312 ');
Why not GBK? If the GBK parameter is not specified, an error is returned if the GBK parameter is forcibly used:
The code is as follows:
Warning: htmlspecialchars (): charset 'gbk' not supported, assuming UTF-8
Change:
The code is as follows:
Htmlspecialchars ($ str, ENT_COMPAT, 'ISO-8859-1 ');
1. 2. change the program, but you can omit a parameter.
You can add
The code is as follows:
Ini_set ('default _ charset', 'gbk ');
Then change
The code is as follows:
Htmlspecialchars ($ str, ENT_COMPAT ,'');
The document contains: 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.
It indicates that the default_charset encoding is used when an empty string is passed in.
1. 3. encapsulate a function... the word htmlspecialchars has never been easy to remember.
The code is as follows:
Function htmlout ($ str ){
Return htmlspecialchars ($ str, ENT_COMPAT, 'ISO-8859-1 ');
}
Then replace them in batches.
2. directly modify the source code and re-compile it! This is also my current online solution.
Modify ext/standard/html. c
About 372 rows
The code is as follows:
/* Default is now UTF-8 */
If (charset_hint = NULL)
Return cs_utf_8;
Change cs_utf_8 to cs_8859_1.
The code is as follows:
/* Default is now UTF-8 */
If (charset_hint = NULL)
Return cs_8859_1;
After compilation, the original program does not need to be adjusted.
For installation method, see: http://www.bitsCN.com/article/63388.htm
What should I do in windows? Here, you can find a way to compile it, which is difficult...
Provide a URL for reference: http://www.bitsCN.com/article/63391.htm
Let's quote one sentence: preparing coffee and cola may take several hours to prepare...