athoshun / html-filter
Remove tags or attributes based on a whitelist from a snippet of well-formatted HTML text using PHP's DOM library
Installs: 26 605
Dependents: 1
Suggesters: 0
Security: 0
Stars: 18
Watchers: 3
Forks: 4
Open Issues: 1
Requires
- php: >=5.3.0
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: 3.7.*
- sami/sami: 1.4.*@dev
This package is not auto-updated.
Last update: 2021-02-05 20:54:08 UTC
README
Remove tags or attributes based on a whitelist from a snippet of somewhat well-formatted HTML text using PHP's DOM library.
Example:
<?php $config = new AthosHun\HTMLFilter\Configuration(); $config->allowTag("p") ->allowAttribute("a", "title") ->allowAttribute("a", "href", "|^https?://.*\$|"); $filter = new AthosHun\HTMLFilter\HTMLFilter(); $html = <<<HTML Lorem ipsum <em>dolor</em> sit amet <p> Consectetur <a href="http://example.com" title="hey!">adipisicing</a> <a href="javascript:alert(42)" onclick="alert(42)">elit</a>. </p> HTML; print $filter->filter($config, $html);
Output:
Lorem ipsum dolor sit amet <p> Consectetur <a href="http://example.com" title="hey!">adipisicing</a> <a>elit</a>. </p>
Installation
Installation is possible via Composer. Create a file named
composer.json
in your project directory with the following contents:
{
"require": {
"athoshun/html-filter": "2.0.*"
}
}
Then as a normal user, issue the following commands:
$ curl http://getcomposer.org/installer | php
$ php composer.phar install