English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The htmlspecialchars() function is used to convert special characters to HTML entities.
string htmlspecialchars ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]] )
Used to convert special characters to HTML entities
It returns the converted string (string).
If the string contains an invalid code unit sequence in the specified encoding encoding, and no ENT_IGNORE or ENT_SUBSTITUTE flag is set, an empty string will be returned.
Predefined characters are:
& (ampersand) becomes &
" (double quote) becomes "
' (single quote) becomes '
< (less than) becomes <
> (greater than) becomes >
注意:To convert special HTML entities back to characters, use htmlspecialchars_decode() Function.
Serial number | Parameters and descriptions |
---|---|
1 | string Required. It contains information about the input string |
2 | flags Optional. Specify how to handle quotes, invalid encoding, and which document type to use.Available quote types:
Invalid encoding:
Additional flags for specifying the document type:
|
3 | encoding 它是一个可选参数,定义了转换字符时使用的编码。 允许的值:
注意:在 PHP 5.4 更早版本,无法被识别的字符集将被忽略并由 ISO-8859-1 代替。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 代替。 |
4 | double_encode 一个指定了是否编码已存在的 HTML 实体的布尔值。
|
试试下面的实例,将预定义的字符转换为 HTML 实体:
<?php //将预定义的字符转换为 HTML 实体,编码双引号和单引号 $input = htmlspecialchars("<a href='https://it.oldtoolbag.com'>w3codebox</a>", &ENT_QUOTES); echo &input; ?>测试看看‹/›
输出结果-
<a href='https://it.oldtoolbag.com'>w3codebox</a>