/home/mjc1/public_html/lib/html_dom/app/index.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
<?php
error_reporting
(E_ALL);
include_once(
'../simple_html_dom.php');

$html file_get_html('google.htm');
//$html = file_get_html('youtube.htm');
//$html = file_get_html('Product.ibatis.xml');


$lang '';
$l=$html->find('html'0);
if (
$l!==null)
    
$lang $l->lang;
if (
$lang!='')
    
$lang 'lang="'.$lang.'"';

$charset $html->find('meta[http-equiv*=content-type]'0);
$target = array();
$query '';

if (isset(
$_REQUEST['query'])) {
    
$query $_REQUEST['query'];
    
$target $html->find($query);
}

function 
stat_dom($dom) {
    
$count_text 0;
    
$count_comm 0;
    
$count_elem 0;
    
$count_tag_end 0;
    
$count_unknown 0;
    
    foreach(
$dom->nodes as $n) {
        if (
$n->nodetype==HDOM_TYPE_TEXT)
            ++
$count_text;
        if (
$n->nodetype==HDOM_TYPE_COMMENT)
            ++
$count_comm;
        if (
$n->nodetype==HDOM_TYPE_ELEMENT)
            ++
$count_elem;
        if (
$n->nodetype==HDOM_TYPE_ENDTAG)
            ++
$count_tag_end;
        if (
$n->nodetype==HDOM_TYPE_UNKNOWN)
            ++
$count_unknown;
    }
    
    echo 
'Total: 'count($dom->nodes).
        
', Text: '.$count_text.
        
', Commnet: '.$count_comm.
        
', Tag: '.$count_elem.
        
', End Tag: '.$count_tag_end.
        
', Unknown: '.$count_unknown;
}

function 
dump_my_html_tree($node$show_attr=true$deep=0$last=true) {
    
$count count($node->nodes);
    if (
$count>0) {
        if(
$last)
            echo 
'<li class="expandable lastExpandable"><div class="hitarea expandable-hitarea lastExpandable-hitarea"></div>&lt;<span class="tag">'.htmlspecialchars($node->tag).'</span>';
        else
            echo 
'<li class="expandable"><div class="hitarea expandable-hitarea"></div>&lt;<span class="tag">'.htmlspecialchars($node->tag).'</span>';
    }
    else {
        
$laststr = ($last===false) ? '' ' class="last"';
        echo 
'<li'.$laststr.'>&lt;<span class="tag">'.htmlspecialchars($node->tag).'</span>';
    }

    if (
$show_attr) {
        foreach(
$node->attr as $k=>$v) {
            echo 
' '.htmlspecialchars($k).'="<span class="attr">'.htmlspecialchars($node->$k).'</span>"';
        }
    }
    echo 
'&gt;';
    
    if (
$node->tag==='text' || $node->tag==='comment') {
        echo 
htmlspecialchars($node->innertext);
        return;
    }

    if (
$count>0) echo "\n<ul style=\"display: none;\">\n";
    
$i=0;
    foreach(
$node->nodes as $c) {
        
$last = (++$i==$count) ? true false;
        
dump_my_html_tree($c$show_attr$deep+1$last);
    }
    if (
$count>0)
        echo 
"</ul>\n";

    
//if ($count>0) echo '&lt;/<span class="attr">'.htmlspecialchars($node->tag).'</span>&gt;';
    
echo "</li>\n";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html <?=$lang?>>
<head>
    <?
        
if ($lang!='')
            echo 
'<meta http-equiv="content-type" content="text/html; charset=utf-8"/>';
        else if (
$charset)
            echo 
$charset;
        else 
            echo 
'<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>';
    
?>
    <title>Simple HTML DOM Query Test</title>
    <link rel="stylesheet" href="js/jquery.treeview.css" />
    <link rel="stylesheet" href="js/screen.css" />
    <style>
        .tag { color: blue; }
        .attr { color: #990033; }
    </style>
    <script src="js/jquery.js" type="text/javascript"></script>
    <script src="js/jquery.treeview.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function(){    
        $("#html_tree").treeview({
            control:"#sidetreecontrol",
            collapsed: true,
            prerendered: true
        });
    });
    </script>
    </head>
    <body>
    <div id="main">
    <h4>Simple HTML DOM Test</h4>
    <form name="form1" method="post" action="">
        find: <input name="query" type="text" size="60" maxlength="60" value="<?=htmlspecialchars($query)?>">
        <input type="submit" name="Submit" value="Go">
    </form>
    <br>
    HTML STAT (<?stat_dom($html);?>)<br>
    <br>
    <div id="sidetreecontrol"><a href="?#">Collapse All</a> | <a href="?#">Expand All</a></div><br>
    <ul class="treeview" id="html_tree">
        <?
            ob_start
();
            foreach(
$target as $e)
                
dump_my_html_tree($etrue);
            
ob_end_flush();
        
?>
    </ul>
</div>
 
</body></html>