PHP htmlspecialchars of Function Instance Code and Usage Encyclopedia

  • 2021-11-01 23:55:57
  • OfStack

Instances

Put the predefined characters " < "(less than) and" > "Convert (greater than) to an HTML entity:


<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars($str);
?>

The HTML output of the above code is as follows (see source code):


<!DOCTYPE html>
<html>
<body>
This is some <b>bold</b> text.
</body>
</html>

Browser output of the above code:

This is some < b > bold < /b > text.

Running an instance

Definition and usage

The htmlspecialchars () function converts predefined characters into HTML entities.

The predefined characters are:

& (And) become &
"(Double quotation marks) become"
'(Single quotation marks) become'
< (Less than) Become <
> (greater than) become >

Tip: To convert special HTML entities back to characters, use the htmlspecialchars_decode () function.

Grammar

htmlspecialchars(string,flags,character-set,double_encode)

参数 描述
string 必需。规定要转换的字符串。
flags

可选。规定如何处理引号、无效的编码以及使用哪种文档类型。

可用的引号类型:

  • ENT_COMPAT - 默认。仅编码双引号。
  • ENT_QUOTES - 编码双引号和单引号。
  • ENT_NOQUOTES - 不编码任何引号。

无效的编码:

  • ENT_IGNORE - 忽略无效的编码,而不是让函数返回1个空的字符串。应尽量避免,因为这可能对安全性有影响。
  • ENT_SUBSTITUTE - 把无效的编码替代成1个指定的带有 Unicode 替代字符 U+FFFD(UTF-8)或者 &#FFFD; 的字符,而不是返回1个空的字符串。
  • ENT_DISALLOWED - 把指定文档类型中的无效代码点替代成 Unicode 替代字符 U+FFFD(UTF-8)或者 &#FFFD;。

规定使用的文档类型的附加 flags:

  • ENT_HTML401 - 默认。作为 HTML 4.01 处理代码。
  • ENT_HTML5 - 作为 HTML 5 处理代码。
  • ENT_XML1 - 作为 XML 1 处理代码。
  • ENT_XHTML - 作为 XHTML 处理代码。
character-set

可选。1个规定了要使用的字符集的字符串。

允许的值:

  • UTF-8 - 默认。ASCII 兼容多字节的 8 位 Unicode
  • ISO-8859-1 - 西欧
  • ISO-8859-15 - 西欧(加入欧元符号 + ISO-8859-1 中丢失的法语和芬兰语字母)
  • cp866 - DOS 专用 Cyrillic 字符集
  • cp1251 - Windows 专用 Cyrillic 字符集
  • cp1252 - Windows 专用西欧字符集
  • KOI8-R - 俄语
  • BIG5 - 繁体中文,主要在台湾使用
  • GB2312 - 简体中文,国家标准字符集
  • BIG5-HKSCS - 带香港扩展的 Big5
  • Shift_JIS - 日语
  • EUC-JP - 日语
  • MacRoman - Mac 操作系统使用的字符集

注释:在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代。

double_encode

可选。布尔值,规定了是否编码已存在的 HTML 实体。

  • TRUE - 默认。将对每个实体进行转换。
  • FALSE - 不会对已存在的 HTML 实体进行编码。

Technical details

返回值:

返回被转换的字符串。

如果 string 包含无效的编码,则返回1个空的字符串,除非设置了 ENT_IGNORE 或者 ENT_SUBSTITUTE 标志。

PHP 版本: 4+
更新日志:

在 PHP 5 中,character-set 参数的默认值改为 UTF-8。

在 PHP 5.4 中,新增了:ENT_SUBSTITUTE、ENT_DISALLOWED、ENT_HTML401、ENT_HTML5、ENT_XML1 和 ENT_XHTML。

在 PHP 5.3 中,新增了 ENT_IGNORE。

在 PHP 5.2.3 中,新增了 double_encode 参数。

在 PHP 4.1 中,新增了 character-set 参数。

More examples

Example 1

Convert 1 predefined characters to HTML entities:


<?php
$str = "Bill & 'Steve'";
echo htmlspecialchars($str, ENT_COMPAT); //  Convert only double quotes 
echo "<br>";
echo htmlspecialchars($str, ENT_QUOTES); //  Convert double quotation marks to single quotation marks 
echo "<br>";
echo htmlspecialchars($str, ENT_NOQUOTES); //  Do not convert any quotation marks 
?>

The HTML output of the above code is as follows (see source code):


<!DOCTYPE html>
<html>
<body>
Bill &amp; 'Steve'<br>
Bill &amp; &#039;Steve&#039;<br>
Bill &amp; 'Steve'
</body>
</html>

Browser output of the above code:

Bill & 'Steve'
Bill & 'Steve'
Bill & 'Steve'

Running an instance

Example 2

Convert double quotation marks to HTML entities:


<?php
$str = 'I love "PHP".';
echo htmlspecialchars($str, ENT_QUOTES); //  Convert double quotation marks to single quotation marks 
?>

The HTML output of the above code is as follows (see source code):


<!DOCTYPE html>
<html>
<body>
I love &quot;PHP&quot;.
</body>
</html>

Browser output of the above code:

I love "PHP".

Let's look at the usage of PHP htmlspecialchars ()

htmlspecialchars() The HTML function converts 1 predefined character to an HTML entity. In fact, the effect of this function is invisible when opening the page in the browser, and it can only be seen when looking at the source code.

• & (And) become &
"(Double quotation marks) become"
'(single quotation mark) becomes'
• < (Less than) Become <
• > (greater than) become >

htmlspecialchars(string,quotestyle,character-set)

quotestyle:

• ES 146EN_ES 147EN-Default. Only double quotation marks are encoded.
ES 149EN_ES 150EN-encoded double and single quotes.
ES 152EN_ES 153EN-No quotation marks are encoded.

Summarize


Related articles: