-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrip.php
310 lines (278 loc) · 7.14 KB
/
Strip.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
/**
* The Ztools_Strip component is used to strip given string from unwanted HTML
* tags and their containing attributes.
* .
*
* @copyright 2010 Artur Gajewski
* @license http://framework.zend.com/license BSD License
* @version Release: @package_version@
* @link http://ztools.arturgajewski.com
* @since Class available since Release 1.0.0
*/
class Ztools_Strip
{
/**
* Data string
*
* @var string $_data
*/
private $_data;
/**
* List of tags to be removed
*
* @var array $_removeTags
*/
private $_removeTags = array();
/**
* List of tags to be removed but content remained
*
* @var array $_removeOnlyTags
*/
private $_removeOnlyTags = array();
/**
* Remove all comment blocks from input
*
* @var boolean $_removeComments
*/
private $_removeComments;
/**
* Remove all empty tag blocks
*
* @var boolean $_removeEmptyBlocks
*/
private $_removeEmptyBlocks;
/**
* Remove all blank lines from the input
*
* @var boolean $_removeBlankLines
*/
private $_removeBlankLines;
/**
* Resulting stripped string
*
* @var string $_result
*/
private $_result;
/**
* Sets the string data to be stripped.
*
* @param string $value
* @return Ztools_Highlight
*/
public function setData($value)
{
$this->_data = $value;
return $this;
}
/**
* Gets the string data to be stripped.
*
* @return $this->_data
*/
public function getData()
{
return $this->_data;
}
/**
* Adds the tag to be removed.
*
* @param mixed $value
* @return Ztools_Highlight
*/
public function addRemoveTag($value)
{
if (is_array($value)) {
foreach ($value as $entry) {
$entry = $this->cleanTag($entry);
$this->_removeTags[] = $entry;
}
} else {
$this->_removeTags[] = $value;
}
return $this;
}
/**
* Gets the array container of added removal tags.
*
* @return array $_removeTags
*/
public function getRemoveTags()
{
return $this->_removeTags;
}
/**
* Adds the tag to be removed while the content
* remained as is.
*
* @param mixed $value
* @return Ztools_Highlight
*/
public function addRemoveOnlyTag($value)
{
if (is_array($value)) {
foreach ($value as $entry) {
$entry = $this->cleanTag($entry);
$this->_removeOnlyTags[] = $entry;
}
} else {
$this->_removeOnlyTags[] = $value;
}
return $this;
}
/**
* Gets the array container of added removal tags
* while content remained.
*
* @return array $_removeOnlyTags
*/
public function getRemoveOnlyTags()
{
return $this->_removeOnlyTags;
}
/**
* Sets a value for removing comment blocks.
*
* @param boolean $value
* @return Ztools_Highlight
*/
public function setRemoveComments($value)
{
$this->_removeComments = $value;
return $this;
}
/**
* Gets the value of comment removal.
*
* @return boolean $_removeComments
*/
public function getRemoveComments()
{
return $this->_removeComments;
}
/**
* Sets a value for removing empty tag blocks.
*
* @param boolean $value
* @return Ztools_Highlight
*/
public function setRemoveEmptyBlocks($value)
{
$this->_removeEmptyBlocks = $value;
return $this;
}
/**
* Gets the value of empty tag block removal.
*
* @return boolean $_removeEmptyBlocks
*/
public function getRemoveEmptyBlocks()
{
return $this->_removeEmptyBlocks;
}
/**
* Sets a value for removing blank lines.
*
* @param boolean $value
* @return Ztools_Highlight
*/
public function setRemoveBlankLines($value)
{
$this->_removeBlankLines = $value;
return $this;
}
/**
* Gets the value of empty tag block removal.
*
* @return boolean $_removeBlankLines
*/
public function getRemoveBlankLines()
{
return $this->_removeBlankLines;
}
/**
* Gets the string representation of the array container of added removed tags.
*
* @return string $tagString
*/
public function getTagsString()
{
$tagString = '';
foreach($this->_removeTags as $tag) {
$tagString .= '<' . $tag . '>';
}
return $tagString;
}
/**
* Gets the result of stripped data.
*
* @return string $_result
*/
public function getResult()
{
return $this->_result;
}
/**
* Clean tag from < and > characters
*
* @param $tag
* @return unknown_type
*/
private function cleanTag($tag)
{
$tag = str_replace('<', '', $tag);
$tag = str_replace('>', '', $tag);
$tag = str_replace('<', '', $tag);
$tag = str_replace('>', '', $tag);
return $tag;
}
/**
* The Constructor
*/
public function __construct()
{
$this->_removeComments = false;
$this->_removeEmptyBlocks = false;
$this->_removeBlankLines = false;
}
/**
* Generates stripped output.
*/
public function strip()
{
$text = $this->_data;
foreach ($this->_removeOnlyTags as $tag) {
$text = preg_replace('/<\/?'.$tag.'[^>]*?>/i', '', $text);
}
$removeTags = '';
foreach ($this->_removeTags as $tag) {
$removeTags .= '<' . $tag . '>';
$text = preg_replace('/<' . $tag . '[\S\s]*?\/>/', '', $text);
$text = preg_replace('/<!--[\S\s]*?-->/', '', $text);
}
$this->_result = $this->strip_selected_tags($text, $removeTags, $stripContent = true);
// Strip blocks that contain no data
if ($this->_removeEmptyBlocks == true) {
$this->_result = preg_replace('/<[^\/>]*>([\s]?)*<\/[^>]*>/', '', $this->_result);
}
// Strip blank lines
if ($this->_removeBlankLines == true) {
$this->_result = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $this->_result);
}
}
function strip_selected_tags($str, $tags = "", $stripContent = false)
{
preg_match_all("/<([^>]+)>/i", $tags, $allTags, PREG_PATTERN_ORDER);
foreach ($allTags[1] as $tag) {
$replace = "%(<$tag.*?>)(.*?)(<\/$tag.*?>)%is";
$replace2 = "%(<$tag.*?>)%is";
if ($stripContent) {
$str = preg_replace($replace,'',$str);
$str = preg_replace($replace2,'',$str);
}
$str = preg_replace($replace,'${2}',$str);
$str = preg_replace($replace2,'${2}',$str);
}
return $str;
}
}