php 使用php删除内联样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2488950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:40:59  来源:igfitidea点击:

Removing inline styles using php

phpinlinestyles

提问by Onion

I am using php to output some rich text. How can I strip out the inline styles completely?

我正在使用 php 输出一些富文本。如何完全去除内联样式?

The text will be pasted straight out of MS Word, or OpenOffice, and into a which uses TinyMCE, a Rich-Text editor which allows you to add basic HTML formatting to the text. However I want to remove the inline styles on the

文本将直接从 MS Word 或 OpenOffice 粘贴到使用 TinyMCE(一种富文本编辑器,允许您向文本中添加基本 HTML 格式)中。但是我想删除上的内联样式

tags (see below), but preserve the

标签(见下文),但保留

tags themselves.

标签本身。

<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>

回答by Jake N

I quickly put this together, but for 'inline styles' (!) you will need something like

我很快把它放在一起,但是对于“内联样式”(!),你需要像

$text = preg_replace('#(<[a-z ]*)(style=("|\')(.*?)("|\'))([a-z ]*>)#', '\1\6', $text);

回答by Collin James

Here is a preg_replace solution I derived from Crozin's answer. This one allows for attributes before and after the style attribute fixing the issue with anchor tags.

这是我从 Crozin 的回答中得出的 preg_replace 解决方案。这个允许在修复锚标记问题的样式属性之前和之后的属性。

$value = preg_replace('/(<[^>]*) style=("[^"]+"|\'[^\']+\')([^>]*>)/i', '', $value);

回答by troelskn

回答by Alon Gouldman

You can also use PHP Simple HTML DOM Parser, as follows:

您还可以使用PHP Simple HTML DOM Parser,如下所示:

$html = str_get_html(SOME_HTML_STRING);

foreach ($html->find('*[style]') as $item) {
   $item->style = null;
}

回答by Crozin

You could use regular expressions:

您可以使用正则表达式:

$text = preg_relace('#<(.+?)style=(:?"|\')?[^"\']+(:?"|\')?(.*?)>#si', '<a\1 \2>', $text);

回答by kasarlaravi

You can use: $content = preg_replace('/style=[^>]*/', '', $content);

您可以使用: $content = preg_replace('/style=[^>]*/', '', $content);

回答by Ghasem Aladaghlou

I am did need to clear style from img tags and did resolved by this code:

我确实需要从 img 标签中清除样式并通过以下代码解决:

$text = preg_replace('#(<img (.*) style=("|\')(.*?)("|\'))([a-z ]*)#', '<img \2\6', $text);
echo  $text;

回答by niggles

Couldn't you just use strip_tags and leave in the tags you want eg <p>, <strong>etc?

你不能只使用 strip_tags 并留下你想要的标签,例如<p>, <strong>等吗?

回答by Sinan

Why don't you just overwrite the tags. So you will have clean tags without inline styling.

你为什么不只是覆盖标签。因此,您将拥有没有内联样式的干净标签。

回答by niggles

I found this class very useful for doing strip attributes (especially where there's crazy MS Word formatting all through the text):

我发现这个类对于处理条带属性非常有用(特别是在整个文本中都有疯狂的 MS Word 格式的地方):

http://semlabs.co.uk/journal/php-strip-attributes-class-for-xml-and-html

http://semlabs.co.uk/journal/php-strip-attributes-class-for-xml-and-html