PHP Classes

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    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 170 All time: 8,816 This week: 136Up
Version License PHP version Categories
strip-tags 1.0.2MIT/X Consortium ...5HTML, PHP 5, Parsers


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
Picture of zinsou A.A.E.Mo´se
  Performance   Level  
Name: zinsou A.A.E.Mo´se is available for providing paid consulting. Contact zinsou A.A.E.Mo´se .
Classes: 50 packages by
Country: Benin Benin
Age: 34
All time rank: 6781 in Benin Benin
Week rank: 341 Up1 in Benin Benin Equal
Innovation award
Innovation award
Nominee: 23x

Winner: 2x



foreach (array(
'DOMNodeRecursiveIterator', 'beforeStrip', 'prepareStrip', 'htmlStripHelper', 'htmlStrip') as $file) {


$x='<script type="text/javascript" async="" src="./Transitioning%20from%20Data%20Developer%20to%20Data%20Scientist%20-%20Statistics%20for%20Data%20Science_files/f.txt"></script>';
$data='aaaaa<?php echo here ; ?><!doctype><html><head>'.$x.'</head><!-- a comment --> <body> <?php echo here ; ?><br><br> <h2 onmousedown="alert(\'keke\');">u</h2><p></p><h2>a</h2></body></html>b2b2 ';

$hstrip=new htmlStrip($data, 'replace', array('<h2>', false));
var_dump($hstrip->go()===strip_tags($data, '<h2>')); //true

$hstrip=new htmlStrip($data);
var_dump($hstrip->go()===strip_tags($data)); //true

$hstrip=new htmlStrip($data, 'replace', array('', true), array('src', true));
var_dump((bool)stripos($hstrip->go(htmlStrip::ATTRIBUTES), 'src')); //false

$hstrip=new htmlStrip($data, 'replace', array('', true), array('src', false));
var_dump((bool)stripos($hstrip->go(htmlStrip::ATTRIBUTES), ' type')); //false
var_dump((bool)stripos($hstrip->go(htmlStrip::ATTRIBUTES), 'src')); //true;

$hstrip=new htmlStrip($data, 'replace', array('<doctypetag>', true));
$hstrip1=new htmlStrip($data, 'replace', array('<!doctype>', true));



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
//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 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  
This week:0
All time:8,816
This week:136Up