Taking care of HTML comments in PHP

A little problem came up with some user submitted content on a platform I’m working with.

A form allows users to submit content with tinyMCE. If the content is pasted from MS Word, the source is then littered with HTML conditional comments that can have a detrimental effect on the the page that returns it.

After discovering what was going on, I thought that the best time to capture the offending content is when the text is submitted. Using a regular expression, I can capture the HTML conditionals as well as remove any unnecessary comments:


function clear_html_comments($html)
{
    return preg_replace('/<!--(.|\s)*?-->/', '', $html);
}

That’s it. Just pass in the content from tinyMCE and it should prevent any content being returned that is in HTML comments.

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
, PHPlist