php htmlentities() 和html_entity_decode()函数使用分析

时间:2016-06-29
php htmlentities函数将html字符转换为HTML实体,那么将html实体转换为html字符就使用html_entity_decode函数,本文章向大家介绍htmlentities() 和html_entity_decode()函数使用方法和基本使用实例,需要的朋友可以参考一下。

其实小编在发表这篇文章之前对htmlentities() 和html_entity_decode()函数也没有过多的研究,每次使用到这两个函数时就去查看手册,但今天小编对这两个函数仔细研究了一下,给大家分享一下我对这两个函数的理解。

htmlentities() 和html_entity_decode()是一对逆函数,htmlentities负责将html字符转换为HTML实体,而html_entity_decode负责将HTML实体转化为html字符:

<?php
$str="what's this";
$en_str=htmlentities($str,ENT_QUOTES);
echo $en_str."<br/>";

echo html_entity_decode($en_str,ENT_QUOTES);

?>

运行后浏览器输出如下:

what's this
what's this

查看页面源码,结果如下:

what&#039;s this<br/>what's this

可以看出html_entity_decode函数将被htmlentities处理过的字符串还原了。