简体   繁体   中英

Remove all empty tags except specified

The following PHP regex removes all empty tags:

#<[^\/>]*>([\s]?)*<\/[^>]*>#u

I want to remove empty tags that do not match:

<div style="clear:both"></div>

I tried:

#^(<div style="clear:both"></div>)<[^\/>]*>([\s]?)*<\/[^>]*>#u

...but it didn't work.

How do I add a negation?

Assuming that it is well-formed and there are no missing end tags, this should do the trick:

<(?!div\s+style=(?:"[^"]*?\bclear:\s*both\b[^"]*"|'[^']*?\bclear:\s*both\b[^']*')\s*>\s*</div>).*?>\s*</.*?>

Make sure to use the case-insensitivity flag too. I would still advise against it, though.

EDIT: I haven't tested my edits, but I'm fairly confident that it's a bit more thorough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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