Skip to content

Commit

Permalink
fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
manuwhat committed Jun 22, 2019
1 parent 57da0ba commit fd2b75a
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 11 deletions.
1 change: 1 addition & 0 deletions .php_cs.cache
@@ -0,0 +1 @@
{"php":"7.1.3","version":"2.14.2:v2.14.2#ff401e58261ffc5934a58f795b3f95b355e276cb","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true},"hashes":{"src\\DOMNodeRecursiveIterator.php":-686522259,"src\\htmlStrip.php":-2048634141,"src\\beforeStrip.php":-91041789,"src\\prepareStrip.php":64860972,"tests\\NamedArgsTest.php":254555693,"src\\htmlstripHelper.php":1910840005,"tests\\htmlStripTest.php":2020517432,"src\\htmlStripHelper.php":1393751100,"htmlstriptestinBrowser.php":-70126872}}
6 changes: 3 additions & 3 deletions README.md
@@ -1,13 +1,13 @@
Strip-tags
==========

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

[![Build Status](https://travis-ci.org/manuwhat/strip-tags.svg?branch=master)](https://travis-ci.org/manuwhat/strip-tags)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/manuwhat/strip-tags/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/manuwhat/strip-tags/?branch=master)
[![Build Status](https://scrutinizer-ci.com/g/manuwhat/strip-tags/badges/build.png?b=master)](https://scrutinizer-ci.com/g/manuwhat/strip-tags/build-status/master)
[![Code Intelligence Status](https://scrutinizer-ci.com/g/manuwhat/strip-tags/badges/code-intelligence.svg?b=master)](https://scrutinizer-ci.com/code-intelligence)

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


**Requires**: PHP 5.3+


Expand Down
28 changes: 28 additions & 0 deletions htmlstriptestinBrowser.php
@@ -0,0 +1,28 @@
<?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 ; ?> <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'),htmlspecialchars($hstrip->go(htmlStrip::ATTRIBUTES)));//true;


7 changes: 4 additions & 3 deletions src/beforeStrip.php
Expand Up @@ -22,12 +22,13 @@ public function __construct($html)

$html='<div>'.str_replace(
array($this->doctype, $this->html_tag, '</html>', $this->head, '</head>', $this->body, '</body>'),
array('<doctypetag '.substr($this->doctype, 10), '<htmltag '.substr($this->html_tag, 5), '</htmltag>', '<headtag '.substr($this->head, 5), '</headtag>', '<bodytag '.substr($this->body, 5), '</bodytag>'),
array('<doctypetag'.substr($this->doctype, 9), '<htmltag '.substr($this->html_tag, 5), '</htmltag>', '<headtag '.substr($this->head, 5), '</headtag>', '<bodytag '.substr($this->body, 5), '</bodytag>'),
$html
).'</doctypetag></div>';

$preprocessed=token_get_all($html);

$HTML=array_filter($preprocessed, function($v) {
$HTML=array_filter($preprocessed, function ($v) {
return is_array($v)&&$v[0]===T_INLINE_HTML;
});
$PHP=array_diff_key($preprocessed, $HTML);
Expand All @@ -36,7 +37,7 @@ public function __construct($html)

protected function setMainTags(&$html)
{
$doctypeOffset=stripos($html, '<!doctype ');
$doctypeOffset=stripos($html, '<!doctype');
$headOffset=stripos($html, '<head');
$htmlTagOffset=stripos($html, '<html');
$bodyOffset=stripos($html, '<body');
Expand Down
4 changes: 2 additions & 2 deletions src/htmlStripHelper.php
Expand Up @@ -444,8 +444,8 @@ protected static function handlePhp($is_php, $domDoc, &$allowed_tags)
protected static function handleMainHtmlTags(&$result, &$allowed_tags)
{
$result=str_replace(
array('<doctypetag','</doctypetag>','<headtag ' ,'</headtag','<htmltag ','</htmltag','<bodytag ','</bodytag'),
array('<!doctype ','','<head ','</head','<html ','</html','<body ','</body'),
array('<doctypetag','</doctypetag>','<headtag' ,'</headtag','<htmltag','</htmltag','<bodytag','</bodytag'),
array('<!doctype','','<head','</head','<html','</html','<body','</body'),
$result
);
if (!isset($allowed_tags['<doctypetag>'])) {
Expand Down
4 changes: 2 additions & 2 deletions src/prepareStrip.php
Expand Up @@ -26,13 +26,13 @@ public function getPrepared()

public function prepareHtml()
{
return array_map(function($v) {
return array_map(function ($v) {
return is_array($v) ? $v[1] : $v;
}, $this->prepocessed->getHTML());
}
public function preparePhp()
{
return array_map(function($v) {
return array_map(function ($v) {
return is_array($v)&&($v[0]===T_OPEN_TAG||$v[0]===T_CLOSE_TAG) ? ($v[0]===T_OPEN_TAG ? '<php>' : '</php>') : (is_array($v) ? $v[1] : $v);
}, $this->prepocessed->getPHP());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/htmlStripTest.php
Expand Up @@ -28,7 +28,7 @@ public function testGo()
$this->assertFalse((bool)stripos($hstrip->go(htmlStrip::ATTRIBUTES), 'src'));

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

0 comments on commit fd2b75a

Please sign in to comment.