Home >Backend Development >PHP Tutorial >Summary of solutions to the problem that htmlspecialchars output is empty under GBK encoding in php5.4 or above, htmlspecialchars_PHP tutorial

Summary of solutions to the problem that htmlspecialchars output is empty under GBK encoding in php5.4 or above, htmlspecialchars_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:58:02973browse

A summary of solutions to the problem that htmlspecialchars output is empty under GBK encoding in php5.4 or above. htmlspecialchars

Upgrading from the old version to php5.4, I am afraid the most troublesome problem is htmlspecialchars. ! Of course, htmlentities will also be affected. However, for Chinese websites, it is more common to use htmlspecialchars, and htmlentities are rarely used.

Maybe foreigners think that web pages should generally be encoded in UTF-8, so they suffer from those Chinese websites that use GB2312, GBK encoding...!

Specific performance:
Copy code The code is as follows:
$str = "The php version of 9enjoy.com is 5.2.10";
echo htmlspecialchars($str);

The output under the gbk character set is empty...under utf-8, the output is normal.

Why? The reason lies in the changes to this function in 5.4.0:
Copy code The code is as follows:
5.4.0 The default value for the encoding parameter was changed to UTF-8.

What was it?
Copy code 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.

It turned out to be ISO-8859-1, but after 5.4 it became utf-8 by default! Then when using this function in Chinese, the output will be blank.

A lot of domestic open source programs will have such problems under 5.4. DISCUZ officials also recommend that users not upgrade to 5.4

Solution:

1. Hardly modify all programs that use htmlspecialchars

1.1 The second $flags parameter defaults to ENT_COMPAT, so it is changed to
Copy code The code is as follows:
htmlspecialchars($str,ENT_COMPAT,'GB2312');

Why not GBK? Because there is no GBK parameter, if you forcibly use GBK, an error will be reported to you:
Copy code The code is as follows:
Warning: htmlspecialchars(): charset `gbk' not supported, assuming utf-8

In order to use GBK, change it to:
Copy code The code is as follows:
htmlspecialchars($str,ENT_COMPAT,'ISO-8859-1');

1.2. The same procedure is changed, but one parameter can be omitted.
You can add
to the head of the web page Copy code The code is as follows:
ini_set('default_charset','gbk');

Then change it to
Copy code The code is as follows:
htmlspecialchars($str,ENT_COMPAT,'');

The document states: 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.
The general meaning is: when an empty string is passed in, the encoding of default_charset is used

1.3. Encapsulate a function... The word htmlspecialchars has always been hard to remember.
Copy code The code is as follows:
function htmlout($str) {
Return htmlspecialchars($str,ENT_COMPAT,'ISO-8859-1');
}

Then go to batch replacement.

2. Modify the source code directly and recompile! This is also the plan I am currently working on online.
Modify ext/standard/html.c
About line 372
Copy code 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
Copy code 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 in any way.
For installation methods, please refer to: http://www.bkjia.com/article/63388.htm

What should I do under Windows? Let's find a way to compile this by ourselves, it's quite difficult...
Provide a URL for reference: http://www.bkjia.com/article/63391.htm
To quote one of his words: Prepare coffee and coke, be prepared, it may take hours...

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/978728.htmlTechArticleSummary of solutions to the problem that htmlspecialchars output is empty under GBK encoding in php5.4 or above, upgrade htmlspecialchars from the old version to php5. 4. I’m afraid the most troublesome issue is htmlspecialchars...
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