htmlentities
将所有适用的字符转换为HTML实体-将字符转换为 HTML 转义字符
htmlentities()
函数把字符转换为 HTML 实体。
提示:要把 HTML 实体转换回字符,请使用 html_entity_decode()
函数。
提示:请使用 get_html_translation_table()
函数来返回 htmlentities()
使用的翻译表。
把字符转换为 HTML 实体:
<?php $str = "<? W3S?h????>"; echo htmlentities($str); ?>
以上代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html> <html> <body> <? W3S?h°°|§> </body> </html>
以上代码的浏览器输出:
<? W3S?h????>
亲自试一试
把字符转换为 HTML 实体:
<?php $str = "Bill & 'Steve'"; echo htmlentities($str, ENT_COMPAT); // 只转换双引号 echo "<br>"; echo htmlentities($str, ENT_QUOTES); // 转换双引号和单引号 echo "<br>"; echo htmlentities($str, ENT_NOQUOTES); // 不转换任何引号 ?>
以上代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html> <html> <body> Bill & 'Steve'<br> Bill & 'Tarzan'<br> Bill & 'Steve' </body> </html>
以上代码的浏览器输出:
Bill & 'Steve' Bill & 'Steve' Bill & 'Steve'
亲自试一试
通过使用西欧字符集,把一些字符转换为 HTML 实体:
<?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 ?>
以上代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html> <html> <body> My name is ?yvind ?sane. I'm Norwegian. </body> </html>
以上代码的浏览器输出:
My name is ?yvind ?sane. I'm Norwegian.
亲自试一试
htmlentities(string,flags,character-set,double_encode)
参数 | 描述 |
---|---|
string | 必需。规定要转换的字符串。 |
flags |
可选。规定如何处理引号、无效的编码以及使用哪种文档类型。 可用的引号类型:
无效的编码:
规定使用的文档类型的附加 flags:
|
character-set |
可选。一个规定了要使用的字符集的字符串。 允许的值:
注释:在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代。 |
double_encode |
可选。布尔值,规定是否编码已存在的 HTML 实体。
|