简体   繁体   English

使用PHP删除大多数内联样式和属性

[英]Removing most inline styles and properties with PHP

This question is related to a similar case, namely Removing inline styles using php 这个问题与类似的情况有关,即使用php删除内联样式

The solution there does not remove ie: <font face="Tahoma" size="4"> 那里的解决方案不会删除,即: <font face="Tahoma" size="4">

But let's say I have a mixed bag of inline styles and properties, like this: 但是,假设我混合使用内联样式和属性,如下所示:

<ul style="padding: 5px; margin: 5px;">
    <li style="padding: 2px;"><div style="border:2px solid green;">Some text</div></li>
    <li style="padding: 2px;"><font face="arial,helvetica,sans-serif" size="2">Some text</font></li>
    <li style="padding: 2px;"><font face="arial,helvetica,sans-serif" size="2">Some text</font></li>  
</ul>

What regExp is needed to achieve this result? 要获得此结果需要什么regExp?

<ul>
    <li><div>Some text</div></li>
    <li><font>Some text</font></li>
    <li><font>Some text</font></li>  
</ul>

As usual, regex isn't ideal for parsing HTML; 像往常一样,正则表达式不是解析HTML的理想选择。 it's very possible you'd be better off with an actual HTML parser. 使用实际的HTML解析器很有可能会更好。

That said... 那个...

$noattributes = preg_replace('/<(\w+) [^>]+>/', '<$1>', $original);

...will replace any opening tags that contain attributes with the corresponding tag w/o attributes. ...将用不带属性的相应标签替换所有包含属性的开头标签。 It might, however, accidentally also hit "tags" that are contained within quoted attributes of other tags (and thus not actually tags themselves). 但是,它也可能偶然打中了包含在其他标签的引用属性中的“标签”(因此实际上并不是标签本身)。 It will also cause problems with self-closing tags (it'll replace <br /> with <br> ) - though this can be avoided if the self-closing tags don't have a space between the tag name and the slash. 这也将导致自闭合标签出现问题(将<br />替换为<br> )-尽管如果自闭合标签在标签名称和斜杠之间没有空格,则可以避免这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM