首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用php删除内联样式

使用php删除内联样式
EN

Stack Overflow用户
提问于 2010-03-22 06:11:19
回答 10查看 25.8K关注 0票数 14

我正在使用php输出一些富文本。怎样才能完全去掉内联样式呢?

文本将直接从MS Word或OpenOffice中粘贴出来,然后粘贴到使用TinyMCE的文本编辑器中。但是,我想要删除

标记(见下文),但保留

标签本身。

代码语言:javascript
复制
<p style="margin-bottom: 0cm;">A patrol of Zograth apes came round the corner, causing Rosette to pull Rufus into a small alcove, where she pressed her body against his. &ldquo;Sorry.&rdquo; She said, breathing warm air onto the shy man's neck. Rufus trembled.</p>
<p style="margin-bottom: 0cm;">&nbsp;</p>
<p style="margin-bottom: 0cm;">Rosette checked the coast was clear and pulled Rufus out of their hidey hole. They watched as the Zograth walked down a corridor, almost out of sight and then collapsed next to a phallic fountain. As their bodies hit the ground, their guns clattered across the floor. Rosette stopped one with her heel and picked it up immediately, tossing the other one to Rufus. &ldquo;Most of these apes seem to be dying, but you might need this, just to give them a helping hand.&rdquo;</p>
EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2010-03-23 07:29:17

我很快就把这些放到了一起,但是对于‘内联样式’(!)你需要像这样的东西

代码语言:javascript
复制
$text = preg_replace('#(<[a-z ]*)(style=("|\')(.*?)("|\'))([a-z ]*>)#', '\\1\\6', $text);
票数 30
EN

Stack Overflow用户

发布于 2013-12-07 04:04:07

这是我从克罗津的答案中得出的一个preg_replace解决方案。它允许在style属性之前和之后使用属性来修复锚点标签的问题。

代码语言:javascript
复制
$value = preg_replace('/(<[^>]*) style=("[^"]+"|\'[^\']+\')([^>]*>)/i', '$1$3', $value);
票数 18
EN

Stack Overflow用户

发布于 2010-03-22 18:14:51

使用HtmlPurifier

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2488950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档