~ubuntu-branches/ubuntu/hardy/php5/hardy-updates

« back to all changes in this revision

Viewing changes to ext/tidy/examples/urlgrab.php

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
    /*
 
4
     * urlgrab.php
 
5
     *
 
6
     * A simple command-line utility to extract all of the URLS contained
 
7
     * within <A HREF> tags from a document.
 
8
     *
 
9
     * NOTE: Only works with tidy for PHP 4.3.x, please see urlgrab5.php for tidy for PHP 5
 
10
     *
 
11
     * By: John Coggeshall <john@php.net>
 
12
     *
 
13
     * Usage: php urlgrab.php <file>
 
14
     *
 
15
     */
 
16
         
 
17
    /* Parse the document */
 
18
    tidy_parse_file($_SERVER['argv'][1]);
 
19
    
 
20
    /* Fix up the document */
 
21
    tidy_clean_repair();
 
22
    
 
23
    /* Get an object representing everything from the <HTML> tag in */
 
24
    $html = tidy_get_html();
 
25
    
 
26
    /* Traverse the document tree */
 
27
    print_r(get_links($html));
 
28
    
 
29
    function get_links($node) {
 
30
        $urls = array();
 
31
        
 
32
        /* Check to see if we are on an <A> tag or not */
 
33
        if($node->id == TIDY_TAG_A) {
 
34
            /* If we are, find the HREF attribute */
 
35
            $attrib = $node->get_attr(TIDY_ATTR_HREF);
 
36
            if($attrib) {
 
37
                /* Add the value of the HREF attrib to $urls */
 
38
                $urls[] = $attrib->value;
 
39
            }
 
40
            
 
41
        }
 
42
        
 
43
        /* Are there any children? */
 
44
        if($node->has_children()) {
 
45
            
 
46
            /* Traverse down each child recursively */
 
47
            foreach($node->children() as $child) {
 
48
                   
 
49
                /* Append the results from recursion to $urls */
 
50
                foreach(get_links($child) as $url) {
 
51
                    
 
52
                    $urls[] = $url;
 
53
                    
 
54
                }
 
55
                
 
56
            }
 
57
        }
 
58
        
 
59
        return $urls;
 
60
    }
 
61
    
 
62
?>  
 
 
b'\\ No newline at end of file'