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 |
可选。规定如何处理引号、无效的编码以及使用哪种文档类型。 可用的引号类型:
无效的编码:
规定使用的文档类型的附加 flags:
|
character-set |
可选。1个规定了要使用的字符集的字符串。 允许的值:
注释:在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代。 |
double_encode |
可选。布尔值,规定了是否编码已存在的 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 & 'Steve'<br>
Bill & 'Steve'<br>
Bill & '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 "PHP".
</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