The translations performed are:
'&' (ampersand) becomes '&'
'"' (double quote) becomes '"' when ENT_NOQUOTES is not set.
''' (single quote) becomes ''' only when ENT_QUOTES is set.
''>' (greater than) becomes '>'
htmlspecialchars 只转化上面这几个html代码,而 htmlentities 却会转化所有的html代码,连同里面的它无法识别的中文字符也给转化了。
我们可以拿一个简单的例子来做比较:
结论是,有中文的时候,最好用 htmlspecialchars ,否则可能乱码
另外参考一下这个自定义函数
function my_excerpt( $html, $len ) {
// $html 应包含一个 HTML 文档。
// 本例将去掉 HTML 标记,javascript 代码
// 和空白字符。还会将一些通用的
// HTML 实体转换成相应的文本。
$search = array ("'<script>]*?>.*?</script>'si", // 去掉 javascript
"']*?>'si", // 去掉 HTML 标记
"'([\r\n])[\s]+'", // 去掉空白字符
"'&(quot|#34);'i", // 替换 HTML 实体
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'(\d+);'e"); // 作为 PHP 代码运行
$replace = array ("",
"",
"\\1",
"\"",
"&",
"">",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(\\1)");
$text = preg_replace ($search, $replace, $html);
$text = trim($text);
return mb_strlen($text) >= $len ? mb_substr($text, 0, $len) : '';
}
htmlspecialchar()函数和htmlentities()函数类似都是把html代码转换,htmlspecialchars_decode是把转化的html的编码转换成转换回来。
我们可以拿一个简单的例子来做比较:
$str='测试';
$transstr = htmlspecialchars($str) ;
echo $transstr . "
";
echo htmlspecialchars_decode($transstr)";
运行上面的代码,就可以看出两者的差别了。
一直都知道 PHP 中的 htmlentities 和 htmlspecialchars 函数都能把 html 中的特殊字符转换成对应的 character entity (不知道怎么翻译),也一直都知道 htmlentities 和 htmlspecialchars 函数有区别,但是一直都用不到这两个函数,也就没去研究过到底有什么区别。
今天用到了,懒得看 PHP 手册里的鸟语,觉得这种问题应该会有人用中文写过,于是 Google 关键词“htmlentities htmlspecialchars”,答案千篇一律。我已经司空见惯了,复制粘贴连小学生都会。经过对比发现,每篇文章大概都包含两部分:
第一部分是引用 PHP 手册的说明:
PHP 手册中对 htmlspecialchars 写道:
The translations performed are:
‘&' (ampersand) becomes ‘&'
‘"' (double quote) becomes ‘"' when ENT_NOQUOTES is not set.
”' (single quote) becomes ‘'' only when ENT_QUOTES is set.
‘‘>' (greater than) becomes ‘>'
这部分无可厚非,但是第二部分的解释却并不怎么正确:
htmlspecialchars 只转化上面这几个html代码,而 htmlentities 却会转化所有的html代码,连同里面的它无法识别的中文字符也给转化了。
我们可以拿一个简单的例子来做比较:
$str='测试页面';
echo htmlentities($str);
// ²âÊÔÒ³Ãæ
$str='测试页面';
echo htmlspecialchars($str);
// 测试页面
?>
结论是,有中文的时候,最好用 htmlspecialchars ,否则可能乱码。
难道 htmlentities 函数只有一个参数吗?当然不是!htmlentities 还有三个可选参数,分别是 $quote_style、 $charset、 $double_encode,手册对 $charset 参数是这样描述的:
Defines character set used in conversion. The default character set is ISO-8859-1.
从上面程序输出的结果判断,$str 是 GB2312 编码的,“测试页面”几个字对应的十六进制值是:
B2 E2 CA D4 D2 B3 C3 E6
然而却被当成 ISO-8859-1 编码来解析:
²âÊÔÒ³Ãæ
正好对应 HTML character entity 里的:
²âÊÔÒ³Ãæ
当然会被 htmlentities 转义掉,但是只要加上正确的编码作为参数,根本就不会出现所谓的中文乱码问题:
$str='测试页面';
echo htmlentities($str, ENT_COMPAT, 'gb2312');
// 测试页面三人成虎,以讹传讹。
结论:htmlentities 和 htmlspecialchars 的区别在于 htmlentities 会转化所有的 html character entity,而htmlspecialchars 只会转化手册上列出的几个 html character entity (也就是会影响 html 解析的那几个基本字符)。一般来说,使用 htmlspecialchars 转化掉基本字符就已经足够了,没有必要使用 htmlentities。实在要使用 htmlentities 时,要注意为第三个参数传递正确的编码。

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools
