PHP Classes
elePHPant
Icontem

PHP Strip HTML Tags: Process HTML to strip tags, attributes or PHP code

Recommend this page to a friend!
  Info   View files Example   View files View files (13)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-08-28 (Less than 1 hour ago) RSS 2.0 feedNot yet rated by the usersTotal: 144 This week: 23All time: 8,781 This week: 9Up
Version License PHP version Categories
strip-tags 1.0.2MIT/X Consortium ...5HTML, PHP 5, Parsers
Description Author

This package can process HTML to strip tags, attributes or PHP code.

It can take a string with HTML and parses it to extract its structure.

The package can either replace or remove given HTML or PHP tags, by passing a list of tags or tag attributes to be removed from the HTML document.

It returns another string the processed HTML after the specified tags and attributes are removed or replaced.

Innovation Award
PHP Programming Innovation award nominee
June 2019
Number 4
Sometimes it is necessary to extract PHP code from script files on which it is embedded in HTML.

This package not only can extract or remove PHP code from HTML, but can also extract or remove HTML code from scripts that mix HTML with PHP.

Manuel Lemos
  Performance   Level  
Name: zinsou A.A.E.Moïse <contact>
Classes: 50 packages by
Country: Benin Benin
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

 

Details

Strip-tags

remove PHP and HTML Tags from a string in a custom and efficient way

Build Status Scrutinizer Code Quality Build Status Code Intelligence Status

Requires: PHP 7.0+

Why strip-tags package if PHP have the strip_tags function ?

you could use it if you :

  1. want to remove tags with some attributes only
  2. want to remove some attributes from some tags
  3. want to strip only HTML not PHP or only PHP not HTML
  4. want to remove completely some tags and from other tags partially remove some of their attributes
  5. 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
  Files folder image Files  
File Role Description
Files folder imagesrc (5 files)
Files folder imagetests (1 file)
Accessible without login Plain text file .cs.php Example Example script
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file htmlstriptestinBrowser.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file BeforeStrip.php Class Class source
  Plain text file DOMNodeRecursiveIterator.php Class Class source
  Plain text file HtmlStrip.php Class Class source
  Plain text file HtmlStripHelper.php Class Class source
  Plain text file PrepareStrip.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file HtmlStripTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:144
This week:23
All time:8,781
This week:9Up