Jump to content

Sanitize Html


ShaolinF

Recommended Posts

Hi Guys

 

What is the best way to validate/sanitize user specified html ?

 

You can use strip_tags and define as the second parameter allowed tags, easily such as: '<p><h1>....'etc. But note this will not remove attributes on allowed tags such as 'onClick' etc.

 

<?php
$text = '<script>alert(\'Omg popup!\');</script><p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";

// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>

 

Result:

Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>

 

Link to comment
https://forums.phpfreaks.com/topic/184899-sanitize-html/#findComment-976078
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.