Htmlspecialchars output under php5.4 or later GBK encoding is empty problem solution Summary

Source: Internet
Author: User
This article mainly introduces how to solve the problem of empty output in htmlspecialchars under GBK encoding of php5.4 or later versions. This article provides a variety of solutions to this problem. For more information, see

This article mainly introduces how to solve the problem of empty output in htmlspecialchars under GBK encoding of php5.4 or later versions. This article provides a variety of solutions to this problem. For more information, see

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? Because there is no GBK parameter, if you use GBK forcibly, an error is returned for you:

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 the installation method, refer:

What should I do in windows? Here, you can find a way to compile it, which is difficult...
Provide a URL for reference:
Let's quote one sentence: preparing coffee and cola may take several hours to prepare...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.