Definition and usage of php htmlentities() function, phphtmlentities
php htmlentities() function converts characters into HTML entities. This article introduces the basics of php htmlentities() function to coders The usage method and examples are introduced. Coders in need can refer to it.
Definition and usage
The
htmlentities() function converts characters into HTML entities.
Tip: To convert HTML entities back to characters, use the html_entity_decode() function.
Tip: Please use the get_html_translation_table() function to return the translation table used by htmlentities().
Grammar
htmlentities(string,flags,character-set,double_encode)
Parameters |
Description |
string |
Required. Specifies the string to be converted. |
flags |
参数 |
描述 |
string |
必需。规定要转换的字符串。 |
flags |
可选。规定如何处理引号、无效的编码以及使用哪种文档类型。
可用的引号类型:
- ENT_COMPAT - 默认。仅编码双引号。
- ENT_QUOTES - 编码双引号和单引号。
- ENT_NOQUOTES - 不编码任何引号。
无效的编码:
- ENT_IGNORE - 忽略无效的编码,而不是让函数返回一个空的字符串。应尽量避免,因为这可能对安全性有影响。
- ENT_SUBSTITUTE - 把无效的编码替代成一个指定的带有 Unicode 替代字符 U FFFD(UTF-8)或者 FFFD; 的字符,而不是返回一个空的字符串。
- 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 |
可选。一个规定了要使用的字符集的字符串。
允许的值:
- 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 实体进行编码。
|
Optional. Specifies how to handle quotes, invalid encodings, and which document type to use.
Available quote types:
- ENT_COMPAT - Default. Only double quotes are encoded.
- ENT_QUOTES - Encodes double and single quotes.
- ENT_NOQUOTES - Do not encode any quotes.
Invalid encoding:
返回值: |
返回被转换的字符串。
如果 string 包含无效的编码,则返回一个空的字符串,除非设置了 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 参数。
|
- ENT_IGNORE - Ignore invalid encodings instead of having the function return an empty string. This should be avoided as this may have an impact on security.
- ENT_SUBSTITUTE - Substitutes an invalid encoding with the specified character with the Unicode replacement character U FFFD (UTF-8) or FFFD; instead of returning an empty string.
- ENT_DISALLOWED - Replaces invalid code points in the specified document type with the Unicode replacement character U FFFD (UTF-8) or FFFD;.
Additional flags specifying the document type to use:
- ENT_HTML401 - Default. Code processed as HTML 4.01.
- ENT_HTML5 - Process code as HTML 5.
- ENT_XML1 - code processed as XML 1.
- ENT_XHTML - as XHTML processing code.
|
character-set |
Optional. A string specifying the character set to be used.
Allowed values:
- UTF-8 - Default. ASCII compatible multi-byte 8-bit Unicode
- ISO-8859-1 - Western Europe
- ISO-8859-15 - Western Europe (adds French and Finnish letters missing from ISO-8859-1 for Euro symbol)
- cp866 - DOS-specific Cyrillic character set
- cp1251 - Cyrillic character set for Windows
- cp1252 - Windows-specific Western European character set
- KOI8-R - Russian
- BIG5 - Traditional Chinese, mainly used in Taiwan
- GB2312 - Simplified Chinese, national standard character set
- BIG5-HKSCS - Big5 with Hong Kong extension
- Shift_JIS - Japanese
- EUC-JP - Japanese
- MacRoman - Character set used by Mac operating systems
Note: In versions prior to PHP 5.4, unrecognized character sets will be ignored and replaced by ISO-8859-1. As of PHP 5.4, unrecognized character sets are ignored and replaced by UTF-8.
|
double_encode |
Optional. Boolean value that specifies whether to encode existing HTML entities.
- TRUE - Default. Each entity will be converted.
- FALSE - Existing HTML entities will not be encoded.
|
Technical details
Return value: |
Returns the converted string.
If string contains an invalid encoding, an empty string is returned unless the ENT_IGNORE or ENT_SUBSTITUTE flag is set.
|
PHP version: |
4 |
Update log: |
In PHP 5, the default value of the character-set parameter is changed to UTF-8.
In PHP 5.4, new: ENT_SUBSTITUTE, ENT_DISALLOWED, ENT_HTML401, ENT_HTML5, ENT_XML1 and ENT_XHTML.
In PHP 5.3, ENT_IGNORE was added.
In PHP 5.2.3, the double_encode parameter is added.
In PHP 4.1, there is a new character-set parameter.
|
Example 1
Convert characters to HTML entities:
<?php
$str = "Bill & 'Steve'";
echo htmlentities($str, ENT_COMPAT); // 只转换双引号
echo "<br>";
echo htmlentities($str, ENT_QUOTES); // 转换双引号和单引号
echo "<br>";
echo htmlentities($str, ENT_NOQUOTES); // 不转换任何引号
?>
The HTML output of the above code is as follows (view source code):
<!DOCTYPE html>
<html>
<body>
Bill & 'Steve'<br>
Bill & 'Tarzan'<br>
Bill & 'Steve'
</body>
</html>
Browser output of the above code:
Bill & 'Steve'
Bill & 'Steve'
Bill & 'Steve'
Example 2
Convert some characters to HTML entities by using the Western European character set:
<?php
$str = "My name is ?yvind ?sane. I'm Norwegian.";
echo htmlentities($str, ENT_QUOTES, "ISO-8859-1");
// Will only convert double quotes (not single quotes), and uses the character-set Western European
?>
The HTML output of the above code is as follows (view source code):
<!DOCTYPE html>
<html>
<body>
My name is Øyvind Åsane. I'm Norwegian.
</body>
</html>
Browser output of the above code:
My name is ?yvind ?sane. I'm Norwegian.
The above definition and usage of the php htmlentities() function is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support Bangkejia.
http://www.bkjia.com/PHPjc/1127919.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1127919.htmlTechArticleDefinition and usage of php htmlentities() function, phphtmlentities php htmlentities() function converts characters into HTML entities, this The article introduces the basic usage of php htmlentities() function to programmers...