php5.4 above version GBK coding htmlspecialchars Output for empty problem solving method Rollup _php Tips

Source: Internet
Author: User

From the old version of the upgrade to php5.4, I am afraid the most troublesome thing is htmlspecialchars this problem! Of course, Htmlentities will also be affected, however, for Chinese stations generally used htmlspecialchars more common, htmlentities very little use.

Perhaps the foreigner thinks the webpage commonly should be the Utf-8 code, then painstakingly those use GB2312,GBK code Chinese station ...!

Specific performance:

Copy Code code as follows:

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

The output in the GBK character set is empty ... utf-8, the output is normal.

Why, because of 5.4.0 's changes to this function:

Copy Code code as follows:

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

What's the original?
Copy Code code 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 was 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 the Chinese use this function to output blank.

Domestic a pile of open source program in 5.4 will have such a problem, DISCUZ officials also advised users not to upgrade to 5.4

Solution:

1. Hard to modify all the procedures used to htmlspecialchars places

1.1 Its second $flags parameter, the default is Ent_compat, so change to

Copy Code code as follows:

Htmlspecialchars ($str, Ent_compat, ' GB2312 ');

Why not GBK? Because there is no GBK this parameter, if forced to use GBK, then the error will show you:
Copy Code code as follows:

Warning:htmlspecialchars (): CharSet ' GBK ' not supported, assuming Utf-8

In order to be able to use GBK, it is changed to:
Copy Code code as follows:

Htmlspecialchars ($str, Ent_compat, ' iso-8859-1 ');

1.2. The same is the change procedure, but can omit one parameter.
Can be added to the head of the page
Copy Code code as follows:

Ini_set (' Default_charset ', ' GBK ');

And then change it into
Copy Code code as follows:

Htmlspecialchars ($str, Ent_compat, "");

There are writes in the document: an empty string activates detection from script encoding (Zend multibyte), Default_charset and current locale (read n L_langinfo () and setlocale ()), in the order. Not recommended.
The idea is that the incoming null string uses the Default_charset encoding

1.3. Encapsulate a function ... Originally htmlspecialchars This word has been difficult to remember.

Copy Code code as follows:

function Htmlout ($STR) {
Return Htmlspecialchars ($str, Ent_compat, ' iso-8859-1 ');
}

Then go to bulk replacement.

2. Directly modify the source code, recompile! This is also the current plan that I do online.
Modify EXT/STANDARD/HTML.C
It's about 372 lines.

Copy Code code as follows:

/* Default is now UTF-8 * *
if (Charset_hint = NULL)
return cs_utf_8;

Change the cs_utf_8 into Cs_8859_1
Copy Code code as follows:

/* Default is now UTF-8 * *
if (Charset_hint = NULL)
return cs_8859_1;

After compiling, the original program does not have to make any adjustments.
Installation method can refer to: http://www.jb51.net/article/63388.htm

What do you do under Windows? This, I think of ways to compile it, difficult than the larger ...
Provide a Web site for reference: http://www.jb51.net/article/63391.htm
Quote: Get ready for coffee, cola, prepare, and maybe 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.