php 处理html标签,如何解决php html标签转换问题
发布日期:2022-02-21 12:50:30 浏览次数:37 分类:技术文章

本文共 1136 字,大约阅读时间需要 3 分钟。

php html标签转换问题的解决办法:1、使用“htmlentities()”函数将html标签转换成特殊字符;2、使用“html_entity_decode()”函数将htmlentities函数转义过的字符串转成html标签。

265babd45184a9ab72bd71ebe4a80b9f.png

很多朋友在写php的时候,难免会遇到需要将html标签进行转义存储。比如存入数据库、xml文件等。而存储进去后,读取出来则需要转换成html输出。网上有许多人编写的转换函数,很长很难懂。其实php早就自带有这样的函数。大可不必自己编写。

下面分别介绍这两个函数。

1.htmlentities()函数:

说明:将html标签转换成特殊字符。例如将

例子:

[PHP] view plaincopy// An imaginary article submission from a bad user

// it will redirect anyone to example.com if the code is run in a browser

$userInput = "I am going to hax0r your site, hahaha!

window.location = 'http://www.example.com/'

'";

//Lets make it safer before we use it

$userInputEntities = htmlentities($userInput);

//Now we can display it

echo $userInputEntities;

由于最近csdn的控件比较垃圾,请将上面的$apos改成单引号。---呼!

上面的语句执行后,将生成下面的结果

[HTML] view plaincopyI am going to hax0r your site, hahaha!

window.location = 'http://www.88web.org/'

'

2.html_entity_decode()函数

说明:将htmlentities()函数转义过的字符串转成html标签。

例子:

[PHP] view plaincopy$orig = "I'll /"walk/" the dog now";

$a = htmlentities($orig);

$b = html_entity_decode($a);

echo $a; // I will "walk" the dog now

echo $b; // I will "walk" the dog now

转载自页面http://www.cankaojishu.com/bcyy/82144.html

转载地址:https://blog.csdn.net/weixin_33958381/article/details/115168165 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:php类中返回类属性值_PHP 获取类名以及类的方法和属性
下一篇:php编写函数6,PHP扩展开发教程6 - 扩展函数的参数类型(2)

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年03月29日 01时45分40秒