TYPO3 CMS  TYPO3_6-2
ParseHTML.php
Go to the documentation of this file.
1 <?php
2 
24 
28  var $_re_flags = "si";
29 
34  "<!--.*?-->|<!\[CDATA\[.*?\]\]>|<script\b(?!:)[^>]*>.*?<\/script>";
35 
39  var $_tag_expr = "<%s%s(?:\s.*?)?%s>";
40 
44  var $_attr_find = '\b([-\w]+)=(".*?"|\'.*?\'|.+?)[\/\s>]';
45 
47  {
48  $this->_attr_find = sprintf("/%s/%s",
49  $this->_attr_find,
50  $this->_re_flags);
51 
52  $this->_removed_re = sprintf("/%s/%s",
53  $this->_removed_re,
54  $this->_re_flags);
55 
56  $this->_entity_replacements = array(
57  'amp' => '&',
58  'lt' => '<',
59  'gt' => '>',
60  'quot' => '"'
61  );
62 
63  $this->_ent_replace =
64  sprintf("&(%s);", implode("|",
65  $this->_entity_replacements));
66  }
67 
77  function removeQuotes($str)
78  {
79  $matches = array();
80  $double = '/^"(.*)"$/';
81  $single = "/^\'(.*)\'$/";
82 
83  if (preg_match($double, $str, $matches)) {
84  return $matches[1];
85  } else if (preg_match($single, $str, $matches)) {
86  return $matches[1];
87  } else {
88  return $str;
89  }
90  }
91 
103  function tagPattern($tag_names, $close, $self_close)
104  {
105  if (is_array($tag_names)) {
106  $tag_names = '(?:'.implode('|',$tag_names).')';
107  }
108  if ($close) {
109  $close = '\/' . (($close == 1)? '' : '?');
110  } else {
111  $close = '';
112  }
113  if ($self_close) {
114  $self_close = '(?:\/\s*)' . (($self_close == 1)? '' : '?');
115  } else {
116  $self_close = '';
117  }
118  $expr = sprintf($this->_tag_expr, $close, $tag_names, $self_close);
119 
120  return sprintf("/%s/%s", $expr, $this->_re_flags);
121  }
122 
134  function getMetaTags($html_string)
135  {
136  $html_string = preg_replace($this->_removed_re,
137  "",
138  $html_string);
139 
140  $key_tags = array($this->tagPattern('html', false, false),
141  $this->tagPattern('head', false, false),
142  $this->tagPattern('head', true, false),
143  $this->tagPattern('html', true, false),
144  $this->tagPattern(array(
145  'body', 'frameset', 'frame', 'p', 'div',
146  'table','span','a'), 'maybe', 'maybe'));
147  $key_tags_pos = array();
148  foreach ($key_tags as $pat) {
149  $matches = array();
150  preg_match($pat, $html_string, $matches, PREG_OFFSET_CAPTURE);
151  if($matches) {
152  $key_tags_pos[] = $matches[0][1];
153  } else {
154  $key_tags_pos[] = null;
155  }
156  }
157  // no opening head tag
158  if (is_null($key_tags_pos[1])) {
159  return array();
160  }
161  // the effective </head> is the min of the following
162  if (is_null($key_tags_pos[2])) {
163  $key_tags_pos[2] = strlen($html_string);
164  }
165  foreach (array($key_tags_pos[3], $key_tags_pos[4]) as $pos) {
166  if (!is_null($pos) && $pos < $key_tags_pos[2]) {
167  $key_tags_pos[2] = $pos;
168  }
169  }
170  // closing head tag comes before opening head tag
171  if ($key_tags_pos[1] > $key_tags_pos[2]) {
172  return array();
173  }
174  // if there is an opening html tag, make sure the opening head tag
175  // comes after it
176  if (!is_null($key_tags_pos[0]) && $key_tags_pos[1] < $key_tags_pos[0]) {
177  return array();
178  }
179  $html_string = substr($html_string, $key_tags_pos[1],
180  ($key_tags_pos[2]-$key_tags_pos[1]));
181 
182  $link_data = array();
183  $link_matches = array();
184 
185  if (!preg_match_all($this->tagPattern('meta', false, 'maybe'),
186  $html_string, $link_matches)) {
187  return array();
188  }
189 
190  foreach ($link_matches[0] as $link) {
191  $attr_matches = array();
192  preg_match_all($this->_attr_find, $link, $attr_matches);
193  $link_attrs = array();
194  foreach ($attr_matches[0] as $index => $full_match) {
195  $name = $attr_matches[1][$index];
196  $value = html_entity_decode(
197  $this->removeQuotes($attr_matches[2][$index]));
198 
199  $link_attrs[strtolower($name)] = $value;
200  }
201  $link_data[] = $link_attrs;
202  }
203 
204  return $link_data;
205  }
206 
217  function getHTTPEquiv($html_string)
218  {
219  $meta_tags = $this->getMetaTags($html_string);
220 
221  if ($meta_tags) {
222  foreach ($meta_tags as $tag) {
223  if (array_key_exists('http-equiv', $tag) &&
224  (in_array(strtolower($tag['http-equiv']),
225  array('x-xrds-location', 'x-yadis-location'))) &&
226  array_key_exists('content', $tag)) {
227  return $tag['content'];
228  }
229  }
230  }
231 
232  return null;
233  }
234 }
235 
getHTTPEquiv($html_string)
Definition: ParseHTML.php:217
tagPattern($tag_names, $close, $self_close)
Definition: ParseHTML.php:103
getMetaTags($html_string)
Definition: ParseHTML.php:134