manuwhat / strip-tags
remove PHP and HTML Tags from a string in a custom and efficient way
dev-master
2019-08-28 08:30 UTC
Requires
- php: ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- phpunit/phpunit: ^6.5.0
This package is auto-updated.
Last update: 2025-05-06 04:46:07 UTC
README
remove PHP and HTML Tags from a string in a custom and efficient way
Requires: PHP 7.0+
Why strip-tags package if PHP have the strip_tags function ?
you could use it if you :
-
want to remove tags with some attributes only
-
want to remove some attributes from some tags
-
want to strip only HTML not PHP or only PHP not HTML
-
want to remove completely some tags and from other tags partially remove some of their attributes
-
want to specify tags to remove instead of tags to keep or tags to keep instead of tags to remove.
How to use it
Require the library by issuing this command:
composer require manuwhat/strip-tags
then you can use it this way:
$data=$data='<!doctype html><html><head>'.$x.$y.'</head><!-- a comment --> <body><?php echo here <br>; ?> <h2 onmousedown="alert(\'keke\');">u</h2><p></p><h2>a</h2></body></html>'; //$data can be a source string or a file $hstrip=new htmlstrip($data,'remove',array(Htmlstrip::getTags(),true));//strip every thing PHP and HTML TAGS $hstrip=new htmlstrip($data,'remove',array('<php>,<script>',true));//remove PHP and sript tags $hstrip=new htmlstrip($data,'replace',array('<php>,<script>',true),array('onemouseover','true'));//remove PHP and sript tags and onemouseover attributes //tags and attributes can be specified as array instead of string var_export(htmlspecialchars($hstrip->go()));//execute //finally you can refine your choices... var_export(htmlspecialchars($hstrip->go(htmlstrip::TAGS)));//act only on tags var_export(htmlspecialchars($hstrip->go(htmlstrip::ATTRIBUTES)));//act only on attributes var_export(htmlspecialchars($hstrip->go(htmlstrip::TAGS_AND_ATTRIBUTES)));//act on tags and attributes this is the default behavior var_export(htmlspecialchars($hstrip->go(htmlstrip::TAGS_WITH_ATTRIBUTES)));//act on tags with some attributes
To run unit tests
composer test