Hoa central
Tag.php
Go to the documentation of this file.
1 <?php
2 
46 import('Filter.Abstract');
47 
63 
69  private $keepTag = array();
70 
76  private $keepAttribute = array();
77 
83  private $keepValue = array();
84 
90  protected $arguments = array(
91  'tag' => 'specify a list of tag to keep.',
92  'attribute' => 'specify a list of attribute to keep.',
93  'value' => 'specify a list of attribute value to keep.'
94  );
95 
96 
97 
105  private function setKeepTag ( $tag = array() ) {
106 
107  if(!is_array($tag))
108  $tag = array($tag);
109 
110  $old = $this->keepTag;
111  $this->keepTag = array_map('strtolower', $tag);
112 
113  return $old;
114  }
115 
123  private function setKeepAttribute ( $attribute = array() ) {
124 
125  if(!is_array($attribute))
126  $attribute = array($attribute);
127 
128  $old = $this->keepAttribute;
129  $this->keepAttribute = array_map('strtolower', $attribute);
130 
131  return $old;
132  }
133 
141  private function setKeepValue ( $value = array() ) {
142 
143  if(!is_array($value))
144  $value = array($value);
145 
146  $old = $this->keepValue;
147  $this->keepValue = array_map('strtolower', $value);
148 
149  return $old;
150  }
151 
158  public function getKeepTag ( ) {
159 
160  return $this->keepTag;
161  }
162 
169  public function getKeepAttribute ( ) {
170 
171  return $this->keepAttribute;
172  }
173 
180  public function getKeepValue ( ) {
181 
182  return $this->keepValue;
183  }
184 
192  protected function stripTag ( $string = '' ) {
193 
194  // 0. captured string ;
195  // 1. previous tag string ;
196  // 2. tag ;
197  // 3. attributes and values ;
198  // 4. tag content ;
199  // 5. next tag string.
200  // 1 2 3 4 5
201  $pattern = '#([^>]+)?<([\w]+)(\s?[^>]*)(?(?<!(?:[/\s?]))>(.*?)(?:</\2>)+|>?)([^<]+)?#Ss';
202  $out = '';
203 
204  if(preg_match_all($pattern, $string, $substring, PREG_SET_ORDER)) {
205 
206  foreach($substring as $tags => $tag) {
207 
208  // filter tags
209  $keepA = in_array(strtolower($tag[2]), $this->getKeepTag());
210 
211  // recursive tags filter
212  if(isset($tag[4]) && preg_match($pattern, $tag[4]))
213  $tag[4] = $this->stripTag($tag[4]);
214  $tag[5] = isset($tag[5]) ? $tag[5] : '';
215 
216  // filter attributes and values
217  // pairs keys match with attributes
218  // unpairs keys match with values
219  $attrval = preg_split('#\s*([^=]*)="([^"]*)"\s*/?\s*#Ss',
220  $tag[3], -1,
221  PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
222 
223  $keepB = true;
224  $keepC = true;
225 
226  if(!empty($attrval))
227  for($i = 0, $max = count($attrval); $i < $max; $i += 2) {
228 
229  $keepB &= in_array(strtolower($attrval[$i]), $this->getKeepAttribute());
230  $keepC &= in_array(strtolower($attrval[$i+1]), $this->getKeepValue());
231  }
232 
233  $out .= $tag[1];
234 
235  if(true == $keepA) {
236 
237  $out .= '<' . $tag[2];
238 
239  if(true == $keepB || true == $keepC)
240  $out .= $tag[3];
241 
242  $out .= '>' .
243  (isset($tag[4])
244  ? $tag[4]
245  : '') .
246  '</' . $tag[2] . '>';
247  }
248 
249  $out .= $tag[5];
250  }
251  }
252 
253  return $out;
254  }
255 
263  public function filter ( $string = null ) {
264 
265  $this->setKeepTag($this->getFilterArgument('tag'));
266  $this->setKeepAttribute($this->getFilterArgument('attribute'));
267  $this->setKeepValue($this->getFilterArgument('value'));
268 
269  return $this->stripTag($string);
270  }
271 }
filter($string=null)
Definition: Tag.php:263
getFilterArgument($name)
Definition: Abstract.php:146
$keepAttribute
Definition: Tag.php:76
getKeepTag()
Definition: Tag.php:158
getKeepValue()
Definition: Tag.php:180
setKeepAttribute($attribute=array())
Definition: Tag.php:123
setKeepValue($value=array())
Definition: Tag.php:141
setKeepTag($tag=array())
Definition: Tag.php:105
getKeepAttribute()
Definition: Tag.php:169
stripTag($string= '')
Definition: Tag.php:192