使用PHP移除内联样式

15

我正在使用PHP输出一些富文本内容。如何完全剥离行内样式?

这些文本将直接从MS Word或OpenOffice粘贴到一个

中,该
使用TinyMCE作为富文本编辑器,允许你向文本添加基本的HTML格式化。然而,我想移除

标签上的行内样式(见下文),但保留这些

标签本身。

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

请重新提出您的问题!我完全不理解。 - Simon
1
你的输入是什么样子的?它是XHTML吗? - mcrumley
10个回答

29

我很快地写了这个,但是如果需要“内联样式”,你需要使用类似于以下的东西

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

@jakenoble - 这对我有效。但是当锚点标签具有内联样式时,它会出现问题。例如 <a style="display:inline;" href="http://abc.com/abc.com">abc</a> 这也影响到 href 部分。你能帮忙吗? - Hacker
1
当样式被转义时,代码无法正常工作。应该重构代码使其正常工作,或将 $text 替换为 stripcslashes($text) - bicycle
更简洁的版本如下: (style=("|\Z)(.*?)("|\Z)),但仅匹配 style="" 部分。 - user1429980
在WordPress内容上对我很有效,谢谢@jake-n - RestlessWeb
当在 style 属性之前有一个属性 (例如 class) 时,这将无法工作。 - Jan Matousek

20

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

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

1
非常好的回答,被接受的解决方案也可以,但在某些标签中删除太多了,比如a(它会删除像href这样的属性)。这个解决方案更好。 - felipep
这个解决方案是最好的,因为它不仅影响到单字母标签(如p、a等),还影响到其他标签(如div、span等)。 - Diego Somar

7

1
我可以使用第三方工具,但我希望有一个更简单的解决方案,比如使用一行正则表达式。 - Onion
1
当然可以。只是要注意风险——使用正则表达式时会有边缘情况。 - troelskn

3
您可以使用PHP Simple HTML DOM解析器,方法如下:
$html = str_get_html(SOME_HTML_STRING);

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

2
您可以使用正则表达式:
$text = preg_relace('#<(.+?)style=(:?"|\')?[^"\']+(:?"|\')?(.*?)>#si', '<a\\1 \\2>', $text);

3
请点击此链接:https://dev59.com/X3I-5IYBdhLWcg3wq6do。 - Alon Gubkin
谢谢,但是那一行不起作用。我收到了这个错误: 解析错误:语法错误,意外的 '['在...(文件名等) - Onion
我忘记在 ' 前面添加转义字符了 ;) - Crozin
嗨 Crozin,不确定我应该在哪里添加转义字符?您是指 \ 吗? - Onion
@Alon,请查看此页面上的第二个答案:https://dev59.com/X3I-5IYBdhLWcg3wq6do#1733489。他有一些已知的HTML代码,这些代码被可靠地生成,因此在这里使用正则表达式并不是一个坏的解决方案。 - nickf

2
您可以使用以下代码:$content = preg_replace('/style=[^>]*/', '', $content);。该代码用于去除$content中的所有style属性,保留其他HTML标记。

0

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


不可以,因为我想保留<p>标签,但是我不想要任何带有内联样式的<p>标签,例如<p style="color:#fff;">。我想要移除的是内联样式,而不是移除<p>标签。 - Onion

0

为什么不直接覆盖标签呢?这样你就可以得到没有内联样式的干净标签了。


0

正确的更新链接 http://semlabs.co.uk/blog/php-strip-attributes-class-xml-html/。 - Elric Wamugu

-1

我需要清除图像标签的样式,并通过以下代码解决:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接