当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP htmlentities()函数用法讲解

PHP htmlentities()函数用法讲解

2019年05月28日  | 移动技术网IT编程  | 我要评论

php htmlentities() 函数

实例

把一些字符转换为 html 实体:

<?php 
$str = "<© w3csçh°°¦§>"; 
echo htmlentities($str); 
?>

上面代码的 html 输出如下(查看源代码):

<!doctype html> 
<html> 
<body> 
<© w3csçh°°¦§> 
</body> 
</html>

上面代码的浏览器输出如下:

<© w3csçh°°¦§>

定义和用法

htmlentities()函数把字符转换为 html 实体。

提示: 要把 html 实体转换回字符,请使用 html_entity_decode() 函数。

提示: 请使用 get_html_translation_table() 函数来返回 htmlentities() 使用的翻译表。

语法

htmlentities( _string,flags,character-set,double_encode_ )

实例 1

把一些字符转换为 html 实体:

<?php 
$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 输出如下(查看源代码):

<!doctype html> 
<html> 
<body> 
jane & 'tarzan'<br> 
jane & 'tarzan'<br> 
jane & 'tarzan' 
</body> 
</html>

上面代码的浏览器输出如下:

jane & 'tarzan' 
jane & 'tarzan' 
jane & 'tarzan'

实例 2

通过使用西欧字符集,把一些字符转换为 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.

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。如果你想了解更多相关内容请查看下面相关链接

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网