Documentation is available at HTML_Functions.php
1 <?php
2 // $Id$
3
4 /**
5 * HTML Functions - common functions for extracting data from HTML
6 *
7 * @author Keyvan Minoukadeh <keyvan@k1m.com>
8 */
9 class HTML_Functions
10 {
11 /**
12 * Find (used by HTML_Parser callback functions)
13 * @var mixed
14 */
15 var $find;
16
17 /**
18 * Result (used by HTML_Parser callback functions)
19 * @var mixed
20 */
21 var $result;
22
23 /**
24 * HTML Parser
25 * @var object HTML_Parser
26 */
27 var $parser;
28
29 function HTML_Functions()
30 {
31 }
32
33 function create_parser()
34 {
35 if (!isset($this->parser)) {
36 // require HTML_Parser from HTTP Navigator classes.
37 require_once(HTTPNAV_ROOT.'HTML_Parser.php');
38 $this->parser =& new HTML_Parser;
39 }
40 $this->parser->set_ignore_tags(null);
41 $this->parser->set_report_tags(null);
42 }
43
44 /**
45 * Reset vars
46 */
47 function reset_vars()
48 {
49 $this->find = null;
50 $this->result = null;
51 }
52
53 /**
54 * Get link url
55 *
56 * Returns URL string for link named <var>$link_name</var>
57 * Example:
58 * <code>
59 * $html = '<a href="http://www.domain.com/bla/bla.html">Great Site</a>';
60 * echo $html_functions->get_link_url($html, 'Great Site');
61 * // prints 'http://www.domain.com/bla/bla.html'
62 * </code>
63 *
64 * @return string returns false if value not found
65 */
66 function get_link_url($html, $link_name)
67 {
68 if (preg_match('/<a\s+.*?href\s*=\s*(["\']?)(.*?)\1[^>]*>'.preg_quote($link_name, '/').'/i', $html, $match)) {
69 return $match[2];
70 }
71 return false;
72 }
73
74
75 /**
76 * Get string after
77 *
78 * Returns string found just after <var>$text_before</var> (after removing
79 * space and quotes)
80 * @return string returns false if value not found
81 */
82 function get_string_after($html, $text_before)
83 {
84 if (preg_match('/'.preg_quote($text_before, '/').'["\' ]*([^"\' <>]+)/i', $html, $match)) {
85 return $match[1];
86 }
87 return false;
88 }
89
90 /**
91 * Get input value
92 *
93 * Get value of input tag whose name attribute value is
94 * <var>$input_name</var>
95 * Example:
96 * <code>
97 * $html = '<form>
98 * <input type="hidden" name="surname" value="Smith" />
99 * </form>';
100 * echo $html_func->get_input_value_by_name($html, 'surname');
101 * // results in 'Smith' being printed
102 * </code>
103 *
104 * @return string returns false if value not found
105 * @see _cb_get_input_value_by_name()
106 */
107 function get_input_value_by_name(&$html, $input_name)
108 {
109 $this->reset_vars();
110 $this->find = $input_name;
111 $this->create_parser();
112 $this->parser->set_report_tags('input');
113 $this->parser->set_start_element_handler(array(&$this, '_cb_get_input_value_by_name'));
114 $this->parser->parse($html, true);
115 if (isset($this->result)) {
116 return $this->result;
117 }
118 return false;
119 }
120
121 /**
122 * Get input name by index
123 *
124 * Get name of input tag <var>$index</var>, where index 1 is the
125 * first input tag.
126 * Example:
127 * <code>
128 * $html = '<form>
129 * <input type="hidden" name="firstname" value="John" />
130 * <input type="hidden" name="surname" value="Smith" />
131 * </form>';
132 * echo $html_func->get_input_name_by_index($html, 2);
133 * // results in 'surname' being printed
134 * </code>
135 *
136 * @return string returns false if value not found
137 * @see _cb_get_input_by_index()
138 */
139 function get_input_name_by_index(&$html, $index)
140 {
141 $index = (int)$index;
142 if (!$index) return false;
143 $this->reset_vars();
144 $this->find = array();
145 $this->find['index'] = (int)$index;
146 $this->find['current_index'] = 0;
147 $this->find['atts'] = 'name';
148 $this->create_parser();
149 $this->parser->set_report_tags('input');
150 $this->parser->set_start_element_handler(array(&$this, '_cb_get_input_by_index'));
151 $this->parser->parse($html, true);
152 if (isset($this->result)) {
153 return $this->result;
154 }
155 return false;
156 }
157
158 /**
159 * Get form action by index
160 *
161 * Get action of form tag <var>$index</var>, where index 1 is the
162 * first form tag.
163 * Example:
164 * <code>
165 * $html = '<form name="aForm" action="http://www.domain.com/submit.php">
166 * <input type="hidden" name="firstname" value="John" />
167 * <input type="hidden" name="surname" value="Smith" />
168 * </form>';
169 * echo $html_func->get_form_action_by_index($html, 1);
170 * // results in 'http://www.domain.com/submit.php' being printed
171 * </code>
172 *
173 * @return string returns false if value not found
174 * @see _cb_get_form_action_by_index()
175 */
176 function get_form_action_by_index(&$html, $index=1)
177 {
178 $index = (int)$index;
179 if (!$index) return false;
180 $this->reset_vars();
181 $this->find = array();
182 $this->find['index'] = (int)$index;
183 $this->find['current_index'] = 0;
184 $this->create_parser();
185 $this->parser->set_report_tags('form');
186 $this->parser->set_start_element_handler(array(&$this, '_cb_get_form_action_by_index'));
187 $this->parser->parse($html, true);
188 if (isset($this->result)) {
189 return $this->result;
190 }
191 return false;
192 }
193
194 /**
195 * Get input value by index
196 *
197 * Get value of input tag <var>$index</var>, where index 1 is the
198 * first input tag.
199 * Example:
200 * <code>
201 * $html = '<form>
202 * <input type="hidden" name="firstname" value="John" />
203 * <input type="hidden" name="surname" value="Smith" />
204 * </form>';
205 * echo $html_func->get_input_name_by_index($html, 2);
206 * // results in 'Smith' being printed
207 * </code>
208 *
209 * @return string returns false if value not found
210 * @see _cb_get_input_by_index()
211 */
212 function get_input_value_by_index(&$html, $index)
213 {
214 $index = (int)$index;
215 if (!$index) return false;
216 $this->reset_vars();
217 $this->find = array();
218 $this->find['index'] = (int)$index;
219 $this->find['current_index'] = 0;
220 $this->find['atts'] = 'value';
221 $this->create_parser();
222 $this->parser->set_report_tags('input');
223 $this->parser->set_start_element_handler(array(&$this, '_cb_get_input_by_index'));
224 $this->parser->parse($html, true);
225 if (isset($this->result)) {
226 return $this->result;
227 }
228 return false;
229 }
230
231 /**
232 * Get input value by index
233 *
234 * Get value of input tag <var>$index</var>, where index 1 is the
235 * first input tag.
236 * Example:
237 * <code>
238 * $html = '<form>
239 * <input type="hidden" name="firstname" value="John" />
240 * <input type="hidden" name="surname" value="Smith" />
241 * </form>';
242 * echo $html_func->get_input_name_by_index($html, 2);
243 * // results in 'hidden' being printed
244 * </code>
245 *
246 * @return string returns false if value not found
247 * @see _cb_get_input_by_index()
248 */
249 function get_input_type_by_index(&$html, $index)
250 {
251 $index = (int)$index;
252 if (!$index) return false;
253 $this->reset_vars();
254 $this->find = array();
255 $this->find['index'] = (int)$index;
256 $this->find['current_index'] = 0;
257 $this->find['atts'] = 'type';
258 $this->create_parser();
259 $this->parser->set_report_tags('input');
260 $this->parser->set_start_element_handler(array(&$this, '_cb_get_input_by_index'));
261 $this->parser->parse($html, true);
262 if (isset($this->result)) {
263 return $this->result;
264 }
265 return false;
266 }
267
268 /**
269 * Get input name and value by index
270 *
271 * Get name and value of input tag <var>$index</var>, where index 1 is the
272 * first input tag.
273 * Example:
274 * <code>
275 * $html = '<form>
276 * <input type="hidden" name="firstname" value="John" />
277 * <input type="hidden" name="surname" value="Smith" />
278 * </form>';
279 * echo $html_func->get_input_name_value($html, 2);
280 * // results in 'surname=Smith' being printed
281 * </code>
282 * Or, if there's more than 1 form...
283 * <code>
284 * $html = '<form name="form1">
285 * <input type="hidden" name="firstname" value="John" />
286 * <input type="hidden" name="surname" value="Smith" />
287 * </form>
288 * <form name="form2">
289 * <input type="hidden" name="age" value="20" />
290 * <input type="hidden" name="sex" value="m" />
291 * </form>';
292 * echo $html_func->get_input_name_value($html, 1, 2);
293 * // results in 'age=20' being printed
294 * </code>
295 *
296 * @return string returns false if value not found
297 * @see _cb_get_input_by_index()
298 */
299 function get_input_name_value(&$html, $index, $form_index=null)
300 {
301 $index = (int)$index;
302 if (!$index) return false;
303 $this->reset_vars();
304 $this->create_parser();
305 $this->find = array();
306 if (isset($form_index)) {
307 $form_index = (int)$form_index;
308 if (!$form_index) return false;
309 $this->find['form_index'] = $form_index;
310 $this->find['current_form_index'] = 0;
311 $this->parser->set_report_tags('form');
312 }
313 $this->find['index'] = (int)$index;
314 $this->find['current_index'] = 0;
315 $this->find['atts'] = 'name_value';
316 $this->parser->set_report_tags('input');
317 $this->parser->set_start_element_handler(array(&$this, '_cb_get_input_by_index'));
318 $this->parser->parse($html, true);
319 if (isset($this->result)) {
320 return $this->result;
321 }
322 return false;
323 }
324
325 // private methods
326
327 /**
328 * Start element handler to find input value
329 */
330 function _cb_get_input_value_by_name(&$parser, $element, $atts)
331 {
332 if (isset($atts['name']) && isset($atts['value'])) {
333 if ($atts['name'] == $this->find) {
334 // found it
335 $this->result = $atts['value'];
336 $parser->eof();
337 }
338 }
339 }
340
341 /**
342 * Start element handler to find input values by index
343 */
344 function _cb_get_input_by_index(&$parser, $element, $atts)
345 {
346 if ($element == 'form') {
347 // increment form index
348 $this->find['current_form_index'] += 1;
349 // reset input index
350 $this->find['current_index'] = 0;
351 if ($this->find['current_form_index'] > $this->find['form_index']) {
352 $parser->eof();
353 }
354 } else {
355 $this->find['current_index'] += 1;
356 if ($this->find['index'] == $this->find['current_index']) {
357 // skip this element if form index isn't one we want
358 if (isset($this->find['form_index']) && ($this->find['form_index'] != $this->find['current_form_index'])) {
359 return;
360 }
361 switch ($this->find['atts']) {
362 case 'name_value':
363 $this->result = @$atts['name'].'='.@$atts['value'];
364 break;
365 case 'name':
366 $this->result = @$atts['name'];
367 break;
368 case 'value':
369 $this->result = @$atts['value'];
370 break;
371 case 'type':
372 $this->result = @$atts['type'];
373 break;
374 }
375 $parser->eof();
376 }
377 }
378 }
379
380 /**
381 * Start element handler to find form action values
382 */
383 function _cb_get_form_action_by_index(&$parser, $element, $atts)
384 {
385 $this->find['current_index'] += 1;
386 if ($this->find['index'] == $this->find['current_index']) {
387 $this->result = @$atts['action'];
388 $parser->eof();
389 }
390 }
391 }
392 ?>
Documention generated on Sat, 19 Jul 2003 03:43:19 +0100 by phpDocumentor 1.2.0