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
Go语言编码解析:UTF-8与GBK对比Go语言编码解析:UTF-8与GBK对比Mar 28, 2024 pm 01:54 PM

Go语言编码解析:UTF-8与GBK对比在Go语言中,处理字符串编码是常见的任务之一。其中,UTF-8和GBK是两种常用的字符编码方式。本文将对UTF-8和GBK进行详细对比,探讨它们的区别和使用方法,并附上具体的代码示例。一、UTF-8与GBK的简介UTF-8:UTF-8是一种可变长度的Unicode编码方式,它可以表示世界上几乎所有语言的字符。UTF-8

dedecms怎么增加多语言dedecms怎么增加多语言Feb 27, 2023 am 09:21 AM

dedecms增加多语言的方法:1、进入dedecms后台,创建封面栏目;2、将封面栏目“常规选项”的文件保存目录设置为cn或者en;3、将封面栏目“高级选项”的列表模板设置为“cn_index.htm”或者“en_index.htm”;4、单独调用每种语言的封面栏目和每种语言下的导航栏目即可。

织梦cms是什么语言写的织梦cms是什么语言写的Feb 21, 2023 am 09:45 AM

织梦cms是用PHP语言写的。织梦CMS(DedeCMS)是一个PHP开源网站管理系统,作用是构建中小型网站;它采用PHP+MySQL技术开发,可同时使用于windows、linux、unix平台。

dedecms是什么语言dedecms是什么语言Feb 24, 2023 am 09:46 AM

dedecms是PHP语言开发的;dedecms中文全称是织梦内容管理系统,是一个PHP开源网站管理系统;dedecms基于PHP和MySQL技术开发,可同时使用于Windows、Linux、Unix平台。

dedecms有移动端吗dedecms有移动端吗Mar 02, 2023 am 09:30 AM

dedecms有移动端,其移动端安装方法是:1、将DATA移到根目录外的安全设置,修改“/m/index.php”代码;2、在“更新主页HTML”中,将“选择主页模板”改为“default/index_m.htm”;3、将首页“图文资讯”的url修改为移动端链接;4、设置移动站可通过二级域名访问即可。

dedecms缩略图报错怎么办dedecms缩略图报错怎么办Feb 27, 2023 am 09:32 AM

dedecms缩略图报错的解决办法:1、将windows目录设置为user读权限;2、将“windows/Temp”设置为user读写权限;3、找到php.ini中的“upload_tmp_dir”目录,然后去掉前面的分号;4、设置地址,赋予user读写权限即可。

DedeCMS创始人林学(IT 柏拉图)逝世DedeCMS创始人林学(IT 柏拉图)逝世Dec 05, 2022 am 11:54 AM

12 月 3 日下午,DedeCMS 创始人林学(IT 柏拉图)因罹患癌症逝世。林学生于 1979 年 10 月 10 日,于 2004 年 8 月编写的 DedeCMS 至今仍有数十万企业、个人站长使用。

2023年织梦dedeCMS视频教程推荐2023年织梦dedeCMS视频教程推荐Oct 25, 2019 pm 01:56 PM

织梦内容管理系统(DedeCMS) 以简单、实用、开源而闻名,是国内最知名的PHP开源网站管理系统,也是使用用户最多的PHP类CMS系统,在经历多年的发展,目前的版本无论在功能,还是在易用性方面,都有了长足的发展和进步。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),