Tuesday 14 August 2018

Remove HTML Comments with PHP

When it comes to sending content to users, I'm of the belief that less is more. There's no reason for HTML comments to be sent down to the user -- they simply bloat the payload. I remove unwanted HTML comments within my WordPress theme, so I thought I'd share the regex that does it:
// Remove unwanted HTML comments 
function remove_html_comments($content = '') { 
 return preg_replace('/<!--(.|\s)*?-->/', '', $content); 
}


That handy function, paired with output buffering, allows me to remove HTML comments from anywhere within the page. Less load, less cruft for mobile users!

0 comments:

Post a Comment