I’d like to remove all the CR, LF and tabs in an HTML string, except when they exist in <pre> and <code> (assuming they are properly closed. Is there a way to do this in PHP?
I’d like to remove all the CR, LF and tabs in an HTML string,
Share
I tried playing around with regex assertions for a while but I don’t think it’s possible that way as PHP doesnt seem to let me use wildcards in the lookahead/lookbehind matches.
Edit
The previous example wasnt going to work with nested elements inside pre & code. This will let you capture the spacings you want to keep, remove all spacing, then put the desired spacing back in:
if the tags are improperly closed, they won’t fit the match pattern and thus will be skipped and have spacing stripped.