求一个php正则表达实现html转义,在线等

levinstong 2012-08-16 10:23:21
数据库的数据存储的是带HTML标签的内容,针对html标签做过转议,如:“<”转义成“<”,现在要把转议过去的部分转议回来,前提是转义这些table,tr,td,th,span,p,I,b,br,ul,li,ol, blockquote,sub,sup,h1,h2,u, strike,a, embed,hr,img的<,>,”,#,/,-,? 如:<table&>转成<table>


不知道大家有没有明白,明白了的帮个忙,谢谢!!!!在线等
...全文
181 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2012-08-16
  • 打赏
  • 举报
回复
方法已经给你了,你自己去建个对照表
levinstong 2012-08-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
示例PHP code
$s =<<< HTML
<table ><tr><td><div> </div></td></tr><t</table>
HTML;
$t = htmlentities($s);

echo preg_replace('/&lt;(\/?(?:tr|td)[^&]*)&gt;/i', '<$1>', $t);
&lt;table &……
[/Quote]
前辈,<,>,”,#,/,-,?这些也要替换
libo_sina 2012-08-16
  • 打赏
  • 举报
回复
strip_tags()去掉 选定的html元素!
xuzuning 2012-08-16
  • 打赏
  • 举报
回复
示例
$s =<<< HTML
<table ><tr><td><div> </div></td></tr><t</table>
HTML;
$t = htmlentities($s);

echo preg_replace('/<(\/?(?:tr|td)[^&]*)>/i', '<$1>', $t);
<table ><tr><td><div> </div></td></tr><t</table>
levinstong 2012-08-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

html_entity_decode()
[/Quote]
这个函数满足不了需求
我要求只转义带table,tr,td,th,span,p,I,b,br,ul,li,ol, blockquote,sub,sup,h1,h2,u, strike,a, embed,hr,img
一起混吧 2012-08-16
  • 打赏
  • 举报
回复
html_entity_decode()
levinstong 2012-08-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

方法已经给你了,你自己去建个对照表
[/Quote]

<?php
$s =<<< HTML
<table style="display:none"><tr><td><div> </div></td></tr><t</table>
HTML;
$t = htmlentities($s);

echo preg_replace('/<(\/?(?:table|tr|td|th|span|p|I|b|br|ul|li|ol|blockquote|sub|sup|h1|h2|u|strike|a|embed|hr|img)[^&]*)>/i', '<$1>', $t);
?>

带有属性的不能正常替 要让标签属性正常使用 正则实在太弱 请教!!!
小在在 2012-08-16
  • 打赏
  • 举报
回复
自已写数组替换吧,示例:

$a=array("<table>");
$b=array("<table>");
$c="333444<table>abcde";
var_dump(str_replace($a,$b,$c));
/*结果:
string(18) "333444<table>abcde"
*/

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧