PHP htmlentities () function
Examples
Some characters into HTML entities:
$str = "<© W3CSch°°¦§>";
echo htmlentities($str);
?>
HTML output of the code above is as follows (view source):
<html>
<body>
<© W3CSçh°°¦§>
</body>
</html>
Browser output of the code above is as follows:
Running instance »
Definition and Usage
htmlentities () function to convert characters to HTML entities.
Tip: put HTML entities back to character, use html_entity_decode () function.
Tip: Use get_html_translation_table () function to return htmlentities () translation table to use.
grammar
参数 | 描述 |
---|---|
string | 必需。规定要转换的字符串。 |
flags | 可选。规定如何处理引号、无效的编码以及使用哪种文档类型。 可用的引号类型:
无效的编码:
规定使用的文档类型的附加 flags:
|
character-set | 可选。一个规定了要使用的字符集的字符串。 允许的值:
注释:在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代。 |
double_encode | 可选。一个规定了是否编码已存在的 HTML 实体的布尔值。
|
technical details
return value: | Returns the converted string. If the string contains invalid code, an empty string is returned, unless the ENT_IGNORE or ENT_SUBSTITUTE flag. |
---|---|
PHP version: | 4+ |
Update log: | In PHP 5, default value character-set parameters to UTF-8. In PHP 5.4, and added: ENT_SUBSTITUTE, ENT_DISALLOWED, ENT_HTML401, ENT_HTML5, ENT_XML1 and ENT_XHTML. In PHP 5.3, add the ENT_IGNORE. In PHP 5.2.3, add the double_encode parameters. In PHP 4.1, add the character-set parameters. |
More examples
Example 1
Some characters into HTML entities:
$str = "Jane & 'Tarzan'";
echo htmlentities($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlentities($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlentities($str, ENT_NOQUOTES); // Does not convert any quotes
?>
HTML output of the code above is as follows (view source):
<html>
<body>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'
</body>
</html>
Browser output of the code above is as follows:
Jane & 'Tarzan'
Jane & 'Tarzan'
Running instance »
Example 2
By using the Western European character set, some characters are converted to HTML entities:
$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
?>
HTML output of the code above is as follows (view source):
<html>
<body>
My name is Øyvind Åsane. I'm Norwegian.
</body>
</html>
Browser output of the code above is as follows:
Running instance »
PHP String Reference