Ratings | | Unique User Downloads | | Download Rankings |
Not yet rated by the users | | Total: 172 | | All time: 8,817 This week: 488 |
|
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
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 |
| |
|
|
Innovation award
Nominee: 23x
Winner: 2x |
|
Example
<?php
$DIR=__DIR__.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR;
foreach (array('DOMNodeRecursiveIterator', 'beforeStrip', 'prepareStrip', 'htmlStripHelper', 'htmlStrip') as $file) {
require_once($DIR.$file.'.php');
}
use EZAMA\htmlStrip;
$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));
var_dump($hstrip->go()===$hstrip1->go());
|
Details
Strip-tags
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
|
Applications that use this package |
|
No pages of applications that use this class were specified.
If you know an application of this package, send a message to the author to add a link here.