PHP code example of webchemistry / html-purifier

1. Go to this page and download the library: Download webchemistry/html-purifier library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

webchemistry / html-purifier example snippets


$rules = new Rules();

$rules->addMulti(
	ElementRule::create('i'),
	ElementRule::create('div'),
	ElementRule::create('strong')
		->addAttributeRule(
			AttributeRule::create('class')
				->allowValue('italic')
		),
	ElementRule::create('script')
		->discard(),
	ElementRule::create('span')
		->addAttributeRule(
			AttributeStyleRule::create()
				->addRule('color', ['white', '#fff', '#fffff'])
		)
);

$purifier = new Purifier($rules);

$purifier->purify('
	<div>
		Lorem ipsum <strong class="italic h2">is simply</strong>
		
		<script>alert("dummy text!")</script>

		<span style="color: #FFF;font-weight: bold">white</span> and <span style="color: red">red</span>

		<br>
	</div>
');