Htmlspecialchars output is empty after php5.4

Source: Internet
Author: User
Upgrade from the old version to php5.4, I am afraid the most troublesome is htmlspecialchars this problem! Of course, Htmlentities will also be affected, however, for the Chinese station generally used htmlspecialchars more common, htmlentities very little use.

Perhaps the foreigner thinks that the webpage generally should be the Utf-8 code, therefore bitter those uses the GB2312,GBK code the Chinese station ...!

Specific performance:

$STR = "PHP version of 9enjoy.com is 5.2.10";
echo Htmlspecialchars ($STR);

GBK Character Set output is empty ... utf-8, output is normal.

Why, the reason is the change of 5.4.0 to this function:

5.4.0 the default value for the encoding parameter is changed to UTF-8.

What was it?

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 are iso-8859-1 in versions of PHP prior to 5.4.0, and UTF-8 from PHP 5.4.0 Onwards.

Turns out to be utf-8! after iso-8859-1,5.4. Then the Chinese use this function to output as blank.

A bunch of open-source programs in China in 5.4 will have such a problem, DISCUZ officials also recommend that users do not upgrade to 5.4.

Solution:
1. Painful modification of all procedures used in htmlspecialchars places

1.1 Its second $flags parameter, default is Ent_compat, so change to
Htmlspecialchars ($str, Ent_compat, ' GB2312 ');
Why not GBK? Because there is no GBK this parameter, if you force the use of GBK, then the error to you to see:
Warning:htmlspecialchars (): CharSet ' GBK ' not supported, assuming Utf-8
To be able to use GBK, change to:
Htmlspecialchars ($str, Ent_compat, ' iso-8859-1 ');

1.2. The same procedure is changed, but one parameter can be omitted.
Can be added to the page header
Ini_set (' Default_charset ', ' GBK ');
And then change into
Htmlspecialchars ($str, Ent_compat, ");
There is a write in the document: an empty string activates detection from script encoding (Zend multibyte), Default_charset and the current locale (see Nl_langinfo () and setlocale ()), in this order. Not recommended.
It probably means: passing in an empty string uses the Default_charset encoding.

1.3. Encapsulate a function ... Originally htmlspecialchars This word has been difficult to remember.
function Htmlout ($STR) {
Return Htmlspecialchars ($str, Ent_compat, ' iso-8859-1 ');
}
Then go to bulk replace.

2. Directly modify the source code, re-compile! That's what I'm doing online right now.
Modify EXT/STANDARD/HTML.C
It's about 372.
/* Default is now UTF-8 */
if (Charset_hint = = NULL)
return cs_utf_8;
Change the cs_utf_8 into a cs_8859_1.
/* Default is now UTF-8 */
if (Charset_hint = = NULL)
return cs_8859_1;
After compiling, the original program will not have to make any adjustments.
Installation method can refer to: http://www.9enjoy.com/linux-upgrade-php54/

What to do under Windows? This, I think of ways to compile it, the difficulty is larger ...
Provide a Web site for reference: http://www.phpvim.net/web/php/build-php5-4-and-xdebug-on-win32.html
Quote a sentence: Get ready for coffee, Coke, get ready, you may have to toss for hours ...
  • 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.