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.

The reason for this is that 5.4.0 's change 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, ");
the document reads: 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 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.
The 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 ...


Htmlspecialchars output is empty after php5.4

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.