Removing most inline styles and properties with PHP [duplicate]
This question is related to a similar case, namely Removing inline styles using php
The solution there does not remove i.e: <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" si开发者_Python百科ze="2">Some text</font></li>
</ul>
What regExp is needed to achieve this result?
<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; it's very possible you'd be better off with an actual HTML parser.
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.
精彩评论