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

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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)