d23fa0138602ca9ca80ea15fffc90695302f9edf.svn-base 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. error_reporting(E_ALL);
  3. include_once('../simple_html_dom.php');
  4. $html = file_get_html('google.htm');
  5. //$html = file_get_html('youtube.htm');
  6. //$html = file_get_html('Product.ibatis.xml');
  7. $lang = '';
  8. $l=$html->find('html', 0);
  9. if ($l!==null)
  10. $lang = $l->lang;
  11. if ($lang!='')
  12. $lang = 'lang="'.$lang.'"';
  13. $charset = $html->find('meta[http-equiv*=content-type]', 0);
  14. $target = array();
  15. $query = '';
  16. if (isset($_REQUEST['query'])) {
  17. $query = $_REQUEST['query'];
  18. $target = $html->find($query);
  19. }
  20. function stat_dom($dom) {
  21. $count_text = 0;
  22. $count_comm = 0;
  23. $count_elem = 0;
  24. $count_tag_end = 0;
  25. $count_unknown = 0;
  26. foreach($dom->nodes as $n) {
  27. if ($n->nodetype==HDOM_TYPE_TEXT)
  28. ++$count_text;
  29. if ($n->nodetype==HDOM_TYPE_COMMENT)
  30. ++$count_comm;
  31. if ($n->nodetype==HDOM_TYPE_ELEMENT)
  32. ++$count_elem;
  33. if ($n->nodetype==HDOM_TYPE_ENDTAG)
  34. ++$count_tag_end;
  35. if ($n->nodetype==HDOM_TYPE_UNKNOWN)
  36. ++$count_unknown;
  37. }
  38. echo 'Total: '. count($dom->nodes).
  39. ', Text: '.$count_text.
  40. ', Commnet: '.$count_comm.
  41. ', Tag: '.$count_elem.
  42. ', End Tag: '.$count_tag_end.
  43. ', Unknown: '.$count_unknown;
  44. }
  45. function dump_my_html_tree($node, $show_attr=true, $deep=0, $last=true) {
  46. $count = count($node->nodes);
  47. if ($count>0) {
  48. if($last)
  49. echo '<li class="expandable lastExpandable"><div class="hitarea expandable-hitarea lastExpandable-hitarea"></div>&lt;<span class="tag">'.htmlspecialchars($node->tag).'</span>';
  50. else
  51. echo '<li class="expandable"><div class="hitarea expandable-hitarea"></div>&lt;<span class="tag">'.htmlspecialchars($node->tag).'</span>';
  52. }
  53. else {
  54. $laststr = ($last===false) ? '' : ' class="last"';
  55. echo '<li'.$laststr.'>&lt;<span class="tag">'.htmlspecialchars($node->tag).'</span>';
  56. }
  57. if ($show_attr) {
  58. foreach($node->attr as $k=>$v) {
  59. echo ' '.htmlspecialchars($k).'="<span class="attr">'.htmlspecialchars($node->$k).'</span>"';
  60. }
  61. }
  62. echo '&gt;';
  63. if ($node->tag==='text' || $node->tag==='comment') {
  64. echo htmlspecialchars($node->innertext);
  65. return;
  66. }
  67. if ($count>0) echo "\n<ul style=\"display: none;\">\n";
  68. $i=0;
  69. foreach($node->nodes as $c) {
  70. $last = (++$i==$count) ? true : false;
  71. dump_my_html_tree($c, $show_attr, $deep+1, $last);
  72. }
  73. if ($count>0)
  74. echo "</ul>\n";
  75. //if ($count>0) echo '&lt;/<span class="attr">'.htmlspecialchars($node->tag).'</span>&gt;';
  76. echo "</li>\n";
  77. }
  78. ?>
  79. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  80. <html <?=$lang?>>
  81. <head>
  82. <?
  83. if ($lang!='')
  84. echo '<meta http-equiv="content-type" content="text/html; charset=utf-8"/>';
  85. else if ($charset)
  86. echo $charset;
  87. else
  88. echo '<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>';
  89. ?>
  90. <title>Simple HTML DOM Query Test</title>
  91. <link rel="stylesheet" href="js/jquery.treeview.css" />
  92. <link rel="stylesheet" href="js/screen.css" />
  93. <style>
  94. .tag { color: blue; }
  95. .attr { color: #990033; }
  96. </style>
  97. <script src="js/jquery.js" type="text/javascript"></script>
  98. <script src="js/jquery.treeview.js" type="text/javascript"></script>
  99. <script type="text/javascript">
  100. $(document).ready(function(){
  101. $("#html_tree").treeview({
  102. control:"#sidetreecontrol",
  103. collapsed: true,
  104. prerendered: true
  105. });
  106. });
  107. </script>
  108. </head>
  109. <body>
  110. <div id="main">
  111. <h4>Simple HTML DOM Test</h4>
  112. <form name="form1" method="post" action="">
  113. find: <input name="query" type="text" size="60" maxlength="60" value="<?=htmlspecialchars($query)?>">
  114. <input type="submit" name="Submit" value="Go">
  115. </form>
  116. <br>
  117. HTML STAT (<?stat_dom($html);?>)<br>
  118. <br>
  119. <div id="sidetreecontrol"><a href="?#">Collapse All</a> | <a href="?#">Expand All</a></div><br>
  120. <ul class="treeview" id="html_tree">
  121. <?
  122. ob_start();
  123. foreach($target as $e)
  124. dump_my_html_tree($e, true);
  125. ob_end_flush();
  126. ?>
  127. </ul>
  128. </div>
  129. </body></html>