d77e785a0126410428f48d4b35bedd93bc16a8e4.svn-base 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>PHP Simple HTML DOM Parser: Manual</title>
  6. <link href="css/default.css" rel="stylesheet" type="text/css">
  7. <link rel="stylesheet" href="css/ui.tabs.css" type="text/css" media="print, projection, screen">
  8. <script type="text/javascript" src="js/jquery-1.2.3.pack.js"></script>
  9. <script type="text/javascript" src="js/ui.tabs.pack.js"></script>
  10. <script language="JavaScript" type="text/JavaScript">
  11. $(document).ready(function(){
  12. $(function() {$('#container_quickstart > ul').tabs();});
  13. $(function() {$('#container_create > ul').tabs();});
  14. $(function() {$('#container_find > ul').tabs();});
  15. $(function() {$('#container_access > ul').tabs();});
  16. $(function() {$('#container_traverse > ul').tabs();});
  17. $(function() {$('#container_dump > ul').tabs();});
  18. $(function() {$('#container_callback > ul').tabs();});
  19. });
  20. </script>
  21. </head>
  22. <body>
  23. <h1><a name="top"></a>PHP Simple HTML DOM Parser Manual</h1>
  24. <div id="content">
  25. <h2>Index</h2>
  26. <ul>
  27. <li><a href="#section_quickstart">Quick Start</a></li>
  28. <li><a href="#section_create">How to create HTML DOM object?</a></li>
  29. <li><a href="#section_find">How to find HTML elements?</a></li>
  30. <li><a href="#section_access">How to access the HTML element's attributes?</a> </li>
  31. <li><a href="#section_traverse">How to traverse the DOM tree?</a></li>
  32. <li><a href="#section_dump">How to dump contents of DOM object?</a></li>
  33. <li><a href="#section_callback">How to customize the parsing behavior?</a></li>
  34. <li><a href="manual_api.htm">API Reference</a></li>
  35. <li><a href="manual_faq.htm">FAQ</a></li>
  36. </ul>
  37. <a name="section_quickstart"></a>
  38. <h2>Quick Start</h2>
  39. <a class="top" href="#top">Top</a>
  40. <div id="container_quickstart">
  41. <ul>
  42. <li><a href="#fragment-11"><span>Get HTML elements</span></a></li>
  43. <li><a href="#fragment-12"><span>Modify HTML elements</span></a></li>
  44. <li><a href="#fragment-13"><span>Extract contents from HTML</span></a></li>
  45. <li><a href="#fragment-14"><span>Scraping Slashdot!</span></a></li>
  46. </ul>
  47. <div id="fragment-11">
  48. <div class="code">
  49. <span class="comment">// Create DOM from URL or file</span><br>
  50. $html = <strong>file_get_html</strong>(<span class="var">'http://www.google.com/'</span>);<br>
  51. <br>
  52. <span class="comment">// Find all images </span><br>
  53. foreach($html-&gt;<strong>find</strong>(<span class="var">'img'</span>) as $element) <br>
  54. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo $element-&gt;<strong>src</strong> . <span class="var">'&lt;br&gt;'</span>;<br>
  55. <br>
  56. <span class="comment">// Find all links </span><br>
  57. foreach($html-&gt;<strong>find</strong>(<span class="var">'a'</span>) as $element) <br>
  58. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo $element-&gt;<strong>href</strong> . <span class="var">'&lt;br&gt;'</span>; </div>
  59. </div>
  60. <div id="fragment-12">
  61. <div class="code">
  62. <span class="comment">// Create DOM from string</span><br>
  63. $html = <strong>str_get_html</strong>(<span class="var">'&lt;div id=&quot;hello&quot;&gt;Hello&lt;/div&gt;&lt;div id=&quot;world&quot;&gt;World&lt;/div&gt;'</span>);<span class="comment"><br>
  64. <br>
  65. </span>
  66. $html-&gt;<strong>find</strong>(<span class="var">'div', 1</span>)-&gt;<strong>class</strong> = <span class="var">'bar'</span>;<br>
  67. <br>
  68. $html-&gt;<strong>find</strong>(<span class="var">'div[id=hello]', 0</span>)-&gt;<strong>innertext</strong> = <span class="var">'foo'</span>;<br>
  69. <br>
  70. echo $html; <span class="comment">// Output: &lt;div id=&quot;hello&quot;&gt;<strong>foo</strong>&lt;/div&gt;&lt;div id=&quot;world&quot; <strong>class=&quot;bar&quot;</strong>&gt;World&lt;/div&gt;</span> </div>
  71. </div>
  72. <div id="fragment-13">
  73. <div class="code"><br>
  74. <span class="comment">// Dump contents (without tags) from HTML</span><br>
  75. echo <strong>file_get_html</strong>(<span class="var">'http://www.google.com/'</span>)-&gt;<strong>plaintext</strong>;
  76. <br>
  77. <br>
  78. </div>
  79. </div>
  80. <div id="fragment-14">
  81. <div class="code">
  82. <span class="comment">// Create DOM from URL</span><br>
  83. $html = <strong>file_get_html</strong>(<span class="var">'http://slashdot.org/'</span>);<br>
  84. <br>
  85. <span class="comment">// Find all article blocks</span><br>
  86. foreach($html-&gt;<strong>find</strong>(<span class="var">'div.article'</span>) as $article) {<br>
  87. &nbsp;&nbsp;&nbsp;&nbsp;$item[<span class="var">'title'</span>]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= $article-&gt;<strong>find</strong>(<span class="var">'div.title'</span>, <span class="var">0</span>)-&gt;<strong>plaintext</strong>;<br>
  88. &nbsp;&nbsp;&nbsp;&nbsp;$item[<span class="var">'intro'</span>]&nbsp;&nbsp;&nbsp;&nbsp;= $article-&gt;<strong>find</strong>(<span class="var">'div.intro'</span>, <span class="var">0</span>)-&gt;<strong>plaintext</strong>;<br>
  89. &nbsp;&nbsp;&nbsp;&nbsp;$item[<span class="var">'details'</span>]&nbsp;= $article-&gt;<strong>find</strong>(<span class="var">'div.details'</span>, <span class="var">0</span>)-&gt;<strong>plaintext</strong>;<br>
  90. &nbsp;&nbsp;&nbsp;&nbsp;$articles[] = $item;<br>
  91. }<br>
  92. <br>
  93. print_r($articles);
  94. </div>
  95. </div>
  96. </div>
  97. <a name="section_create"></a>
  98. <h2>How to create HTML DOM object?</h2>
  99. <a class="top" href="#top">Top</a>
  100. <div id="container_create">
  101. <ul>
  102. <li><a href="#frag_create_quick"><span>Quick way</span></a></li>
  103. <li><a href="#frag_create_oo"><span>Object-oriented way</span></a></li>
  104. </ul>
  105. <div id="frag_create_quick">
  106. <div class="code"><span class="comment">// Create a DOM object from a string</span><br>
  107. $html = <strong>str_get_html</strong>(<span class="var">'&lt;html&gt;&lt;body&gt;Hello!&lt;/body&gt;&lt;/html&gt;'</span>);<br>
  108. <br>
  109. <span class="comment">// Create a DOM object from a URL</span><br>
  110. $html = <strong>file_get_html</strong>(<span class="var">'http://www.google.com/'</span>);<br>
  111. <br>
  112. <span class="comment">// Create a DOM object from a HTML file</span><br>
  113. $html = <strong>file_get_html</strong>(<span class="var">'test.htm'</span>);<span class="comment"><br>
  114. </span></div>
  115. </div>
  116. <div id="frag_create_oo">
  117. <div class="code"><span class="comment">// Create a DOM object</span><br>
  118. $html = new <strong>simple_html_dom</strong>();<br>
  119. <br>
  120. <span class="comment">// Load HTML from a string</span><br>
  121. $html-&gt;<strong>load</strong>(<span class="var">'&lt;html&gt;&lt;body&gt;Hello!&lt;/body&gt;&lt;/html&gt;'</span>);<br>
  122. <br>
  123. <span class="comment">// Load HTML from a URL </span> <br>
  124. $html-&gt;<strong>load_file</strong>(<span class="var">'http://www.google.com/'</span>);<br>
  125. <br>
  126. <span class="comment">// Load HTML from a HTML file</span> <br>
  127. $html-&gt;<strong>load_file</strong>(<span class="var">'test.htm'</span>);</div>
  128. </div>
  129. </div>
  130. <a name="section_find"></a>
  131. <h2>How to find HTML elements?</h2>
  132. <a class="top" href="#top">Top</a>
  133. <div id="container_find">
  134. <ul>
  135. <li><a href="#frag_find_basic"><span>Basics</span></a></li>
  136. <li><a href="#frag_find_advanced"><span>Advanced</span></a></li>
  137. <li><a href="#frag_find_chain"><span>Descendant selectors</span></a></li>
  138. <li><a href="#frag_find_nested"><span>Nested selectors</span></a></li>
  139. <li><a href="#frag_find_attr"><span>Attribute Filters</span></a></li>
  140. <li><a href="#frag_find_textcomment"><span>Text &amp; Comments</span></a></li>
  141. </ul>
  142. <div id="frag_find_basic">
  143. <div class="code"> <span class="comment">// Find all <strong>anchors</strong>, returns a <strong>array</strong> of element objects</span><br>
  144. $ret = $html-&gt;find(<span class="var">'<strong>a</strong>'</span>);<br>
  145. <br>
  146. <span class="comment">// Find <strong>(N)th</strong> <strong>anchor</strong>, returns element object or <strong>null</strong> if not found</span> <span class="comment">(zero based)</span><br>
  147. $ret = $html-&gt;find(<span class="var">'<strong>a</strong>', <strong>0</strong></span>);<br>
  148. <br>
  149. <span class="comment">// Find <strong>lastest</strong> <strong>anchor</strong>, returns element object or <strong>null</strong> if not found</span> <span class="comment">(zero based)</span><br>
  150. $ret = $html-&gt;find(<span class="var">'<strong>a</strong>', <strong>-1</strong></span>); <br>
  151. <br>
  152. <span class="comment">// Find all <strong>&lt;div&gt;</strong> with the <strong>id</strong> attribute</span><br>
  153. $ret = $html-&gt;find(<span class="var">'<strong>div[id]</strong>'</span>);<br>
  154. <br>
  155. <span class="comment">// Find all <strong>&lt;div&gt;</strong> which attribute <strong>id=foo</strong></span><br>
  156. $ret = $html-&gt;find(<span class="var">'<strong>div[id=foo]</strong>'</span>); <br>
  157. </div>
  158. </div>
  159. <div id="frag_find_advanced">
  160. <div class="code"><span class="comment">// Find all element which <strong>id</strong>=foo</span><br>
  161. $ret = $html-&gt;find(<span class="var">'<strong>#foo</strong>'</span>);<br>
  162. <br>
  163. <span class=comment>// Find all element which <strong>class</strong>=foo</span><br>
  164. $ret = $html-&gt;find(<span class=var>'<strong>.foo</strong>'</span>);<br>
  165. <br>
  166. <span class="comment">// Find all element has attribute<strong> id</strong></span><br>
  167. $ret = $html-&gt;find(<span class="var">'<strong>*[id]</strong>'</span>); <br>
  168. <br>
  169. <span class="comment">// Find all <strong>anchors</strong> and <strong>images</strong> </span><br>
  170. $ret = $html-&gt;find(<span class="var">'<strong>a, img</strong>'</span>); <br>
  171. <br>
  172. <span class="comment">// Find all <strong>anchors</strong> and <strong>images</strong> with the "title" attribute</span><br>
  173. $ret = $html-&gt;find(<span class="var">'<strong>a[title], img[title]</strong>'</span>);<br>
  174. </div>
  175. </div>
  176. <div id="frag_find_attr">
  177. <div class="code">
  178. Supports these operators in attribute selectors:<br><br>
  179. <table cellpadding="1" cellspacing="1">
  180. <tr>
  181. <th width="25%">Filter</th>
  182. <th width="75%">Description</th>
  183. </tr>
  184. <tr>
  185. <td>[attribute]</td>
  186. <td>Matches elements that <strong>have</strong> the specified attribute.</td>
  187. </tr>
  188. <tr>
  189. <td>[!attribute]</td>
  190. <td>Matches elements that <strong>don't have</strong> the specified attribute.</td>
  191. </tr>
  192. <tr>
  193. <td>[attribute=value]</td>
  194. <td>Matches elements that have the specified attribute with a <strong>certain value</strong>.</td>
  195. </tr>
  196. <tr>
  197. <td>[attribute!=value]</td>
  198. <td>Matches elements that <strong>don't have</strong> the specified attribute with a certain value.</td>
  199. </tr>
  200. <tr>
  201. <td>[attribute^=value]</td>
  202. <td>Matches elements that have the specified attribute and it <strong>starts</strong> with a certain value.</td>
  203. </tr>
  204. <tr>
  205. <td>[attribute$=value]</td>
  206. <td>Matches elements that have the specified attribute and it <strong>ends</strong> with a certain value.</td>
  207. </tr>
  208. <tr>
  209. <td>[attribute*=value]</td>
  210. <td>Matches elements that have the specified attribute and it <strong>contains</strong> a certain value.</td>
  211. </tr>
  212. </table>
  213. </div>
  214. </div>
  215. <div id="frag_find_chain">
  216. <div class="code"><span class="comment">// Find all &lt;li&gt; in &lt;ul&gt; </span><br>
  217. $es = $html-&gt;find(<span class="var">'<strong>ul li</strong>'</span>);<br>
  218. <br>
  219. <span class="comment">// Find Nested &lt;div&gt; </span><span class="comment">tags</span><br>
  220. $es = $html-&gt;find(<span class="var">'<strong>div div div</strong>'</span>); <br>
  221. <br>
  222. <span class="comment">// Find all &lt;td&gt; in &lt;table&gt; which class=hello </span><br>
  223. $es = $html-&gt;find(<span class="var">'<strong>table.hello td</strong>'</span>);<br>
  224. <br>
  225. <span class="comment">// Find all td tags with attribite align=center in table tags </span><br>
  226. $es = $html-&gt;find(<span class="var">''<strong>table</strong><strong> td[align=center]</strong>'</span>);<br>
  227. </div>
  228. </div>
  229. <div id="frag_find_textcomment">
  230. <div class="code"><span class="comment"> // Find all text blocks </span><br>
  231. $es = $html-&gt;find(<span class="var">'<strong>text</strong>'</span>);<br>
  232. <br>
  233. <span class="comment">// Find all comment (&lt;!--...--&gt;) blocks </span><br>
  234. $es = $html-&gt;find(<span class="var">'<strong>comment</strong>'</span>);<br>
  235. </div>
  236. </div>
  237. <div id="frag_find_nested">
  238. <div class="code"> <span class="comment">// Find all &lt;li&gt; in &lt;ul&gt; </span><br>
  239. foreach($html-&gt;find(<span class="var">'<strong>ul</strong>'</span>) as $ul) <br>
  240. {<br>
  241. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach($ul-&gt;find(<span class="var">'<strong>li</strong>'</span>) as $li) <br>
  242. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
  243. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="comment">// do something...</span><br>
  244. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
  245. }<br>
  246. <br>
  247. <span class="comment">// Find first &lt;li&gt; in first &lt;ul&gt;</span> <br>
  248. $e = $html-&gt;find(<span class="var">'<strong>ul</strong>', <strong>0</strong></span>)-&gt;find(<span class="var">'<strong>li</strong>', <strong>0</strong></span>);<br>
  249. </div>
  250. </div>
  251. </div>
  252. <a name="section_access"></a>
  253. <h2>How to access the HTML element's attributes?</h2>
  254. <a class="top" href="#top">Top</a>
  255. <div id="container_access">
  256. <ul>
  257. <li><a href="#frag_access_attr"><span>Get, Set and Remove attributes</span></a></li>
  258. <li><a href="#frag_access_special"><span>Magic attributes</span></a></li>
  259. <li><a href="#frag_access_tips"><span>Tips</span></a></li>
  260. </ul>
  261. <div id="frag_access_attr">
  262. <div class="code">
  263. <span class="comment">// <strong>Get</strong> a attribute ( If the attribute is <strong>non-value</strong> attribute (eg. checked, selected...), it will returns <strong>true</strong> or <strong>false</strong>)</span><br>
  264. $value = $e-&gt;<strong>href</strong>;<br>
  265. <br>
  266. <span class="comment">// <strong>Set</strong> a attribute(If the attribute is <strong>non-value</strong> attribute (eg. checked, selected...), set it's value as <strong>true</strong> or <strong>false</strong>)</span><br>
  267. $e-&gt;<strong>href</strong> = <span class="var">'my link'</span>;<br>
  268. <br>
  269. <span class="comment">// <strong>Remove</strong> a attribute, set it's value as null! </span><br>
  270. $e-&gt;<strong>href</strong> = <strong><span class="var">null</span></strong>;<br>
  271. <br>
  272. <span class="comment">// <strong>Determine</strong> whether a attribute exist?</span> <br>
  273. if(isset($e-&gt;<strong>href</strong>)) <br>
  274. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo <span class="var">'href exist!'</span>;<br>
  275. </div>
  276. </div>
  277. <div id="frag_access_special">
  278. <div class="code"> <span class="comment">// Example</span><br>
  279. <span class="hl-var">$</span><span class="hl-code">html = </span>str_get_html<span class="hl-brackets">(</span><span class="var">&quot;&lt;div&gt;foo &lt;b&gt;bar&lt;/b&gt;&lt;/div&gt;&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;</span> <br>
  280. $e = $html-&gt;find(<span class="var">&quot;div&quot;</span>, <span class="var">0</span>);<br>
  281. <br>
  282. echo $e-&gt;<strong>tag</strong>; <span class="comment">// Returns: &quot; <strong>div</strong>&quot;</span><br>
  283. echo $e-&gt;<strong>outertext</strong>; <span class="comment">// Returns: &quot; <strong>&lt;div&gt;foo &lt;b&gt;bar&lt;/b&gt;&lt;/div&gt;</strong>&quot;</span><br>
  284. echo $e-&gt;<strong>innertext</strong>; <span class="comment">// Returns: &quot; <strong>foo &lt;b&gt;bar&lt;/b&gt;</strong>&quot;</span><br>
  285. echo $e-&gt;<strong>plaintext</strong>; <span class="comment">// Returns: &quot; <strong>foo </strong><strong>bar</strong>&quot;<br>
  286. <br>
  287. </span>
  288. <table cellspacing="1" cellpadding="1">
  289. <tr bgcolor="#CCCCCC">
  290. <th width="25%">Attribute Name</th>
  291. <th width="75%">Usage</th>
  292. </tr>
  293. <tr>
  294. <td>$e-&gt;<strong>tag</strong></td>
  295. <td>Read or write the <strong>tag name</strong> of element.</td>
  296. </tr>
  297. <tr>
  298. <td>$e-&gt;<strong>outertext</strong></td>
  299. <td>Read or write the <strong>outer HTML text </strong> of element.</td>
  300. </tr>
  301. <tr>
  302. <td>$e-&gt;<strong>innertext</strong></td>
  303. <td>Read or write the <strong>inner HTML text </strong> of element.</td>
  304. </tr>
  305. <tr>
  306. <td>$e-&gt;<strong>plaintext</strong></td>
  307. <td>Read or write the <strong>plain text </strong> of element.</td>
  308. </tr>
  309. </table>
  310. </div>
  311. </div>
  312. <div id="frag_access_tips">
  313. <div class="code"><span class="comment">// <strong>Extract</strong> contents from HTML </span><br>
  314. echo <strong>$html</strong>-&gt;<strong>plaintext</strong>;<br>
  315. <br>
  316. <span class="comment">
  317. // <strong>Wrap</strong> a element</span><br>
  318. $e-&gt;<strong>outertext</strong> = <span class="var">'&lt;div class=&quot;wrap&quot;&gt;'</span> . $e-&gt;<strong>outertext</strong> . <span class="var">'&lt;div&gt;</span>';<br>
  319. <br>
  320. <span class="comment">// <strong>Remove</strong> a element, set it's outertext as an empty string </span><br>
  321. $e-&gt;<strong>outertext</strong> = <span class="var">''</span>;<br>
  322. <br>
  323. <span class="comment">// <strong>Append</strong> a element</span><br>
  324. $e-&gt;<strong>outertext</strong> = $e-&gt;<strong>outertext</strong> . <span class="var">'&lt;div&gt;foo</span><span class="var">&lt;div&gt;</span>';<br>
  325. <br>
  326. <span class="comment">// <strong>Insert</strong> a element</span><br>
  327. $e-&gt;<strong>outertext</strong> = <span class="var">'&lt;div&gt;foo</span><span class="var">&lt;div&gt;</span>' . $e-&gt;<strong>outertext</strong>;<br>
  328. </div>
  329. </div>
  330. </div>
  331. <a name="section_traverse"></a>
  332. <h2>How to traverse the DOM tree?</h2>
  333. <a class="top" href="#top">Top</a>
  334. <div id="container_traverse">
  335. <ul>
  336. <li><a href="#frag_traverse_background"><span>Background Knowledge</span></a></li>
  337. <li><a href="#frag_traverse_traverse"><span>Traverse the DOM tree</span></a></li>
  338. </ul>
  339. <div id="frag_traverse_background">
  340. <div class="code"> <span class="comment">// If you are not so familiar with HTML DOM, check this <a href="http://php.net/manual/en/book.dom.php" target="_blank"><span class="var">link</span></a> to learn more... </span><br>
  341. <br>
  342. <span class="comment">// Example</span><br>
  343. echo $html-&gt;<strong>find</strong>(<span class="var">&quot;#div1&quot;, 0</span>)-&gt;<strong>children</strong>(<span class="var">1</span>)-&gt;<strong>children</strong>(<span class="var">1</span>)-&gt;<strong>children</strong>(<span class="var">2</span>)-&gt;<span class="var">id</span>;<br>
  344. <span class="comment">// or</span> <br>
  345. echo $html-&gt;<strong>getElementById</strong>(<span class="var">&quot;div1&quot;</span>)-&gt;<strong>childNodes</strong>(<span class="var">1</span>)-&gt;<strong>childNodes</strong>(<span class="var">1</span>)-&gt;<strong>childNodes</strong>(<span class="var">2</span>)-&gt;<strong>getAttribute</strong>(<span class="var">'id'</span>); </div>
  346. </div>
  347. <div id="frag_traverse_traverse">
  348. <div class="code">You can also call methods with <a href="manual_api.htm#camel"><span class="var">Camel naming convertions</span></a>.<br>
  349. <table cellspacing="1" cellpadding="1">
  350. <tr>
  351. <th> Method </th>
  352. <th> Description</th>
  353. </tr>
  354. <tr>
  355. <td>
  356. <div class="returns">mixed</div>$e-&gt;<strong>children</strong> ( <span class="var">[int $index]</span> ) </td>
  357. <td>Returns the Nth <strong>child object</strong> if <strong>index</strong> is set, otherwise return an <strong>array of children</strong>. </td>
  358. </tr>
  359. <tr>
  360. <td>
  361. <div class="returns">element</div>$e-&gt;<strong>parent</strong> () </td>
  362. <td>Returns the <strong>parent</strong> of element. </td>
  363. </tr>
  364. <tr>
  365. <td>
  366. <div class="returns">element</div>$e-&gt;<strong>first_child</strong> () </td>
  367. <td>Returns the <strong>first child</strong> of element, or <strong>null</strong> if not found. </td>
  368. </tr>
  369. <tr>
  370. <td>
  371. <div class="returns">element</div>$e-&gt;<strong>last_child</strong> () </td>
  372. <td>Returns the <strong>last child</strong> of element, or <strong>null</strong> if not found. </td>
  373. </tr>
  374. <tr>
  375. <td>
  376. <div class="returns">element</div>$e-&gt;<strong>next_sibling</strong> () </td>
  377. <td>Returns the <strong>next sibling</strong> of element, or<strong> null</strong> if not found. </td>
  378. </tr>
  379. <tr>
  380. <td>
  381. <div class="returns">element</div>$e-&gt;<strong>prev_sibling</strong> () </td>
  382. <td>Returns the <strong>previous sibling</strong> of element, or <strong>null</strong> if not found. </td>
  383. </tr>
  384. </table>
  385. </div>
  386. </div>
  387. </div>
  388. <a name="section_dump"></a>
  389. <h2>How to dump contents of DOM object?</h2>
  390. <a class="top" href="#top">Top</a>
  391. <div id="container_dump">
  392. <ul>
  393. <li><a href="#frag_dump_quick"><span>Quick way</span></a></li>
  394. <li><a href="#frag_dump_oo"><span>Object-oriented way</span></a></li>
  395. </ul>
  396. <div id="frag_dump_oo">
  397. <div class="code"><span class="comment">// </span><span class="comment">Dumps the internal DOM tree back into string </span><br>
  398. $str = $html-&gt;<strong>save</strong>();<br>
  399. <br>
  400. <span class="comment">// Dumps the internal DOM tree back into a file</span> <br>
  401. $html-&gt;<strong>save</strong>(<span class="var">'result.htm'</span>);</div>
  402. </div>
  403. <div id="frag_dump_quick">
  404. <div class="code"><span class="comment">// </span><span class="comment">Dumps the internal DOM tree back into string </span><br>
  405. $str = $html;<br>
  406. <br>
  407. <span class="comment">// Print it!</span><br>
  408. echo $html; <br>
  409. </div>
  410. </div>
  411. </div>
  412. <a name="section_callback"></a>
  413. <h2>How to customize the parsing behavior?</h2>
  414. <a class="top" href="#top">Top</a>
  415. <div id="container_callback">
  416. <ul>
  417. <li><a href="#frag_callback"><span>Callback function</span></a></li>
  418. </ul>
  419. <div id="frag_callback">
  420. <div class="code"><span class="comment">// Write a function with parameter &quot;<strong>$element</strong>&quot;</span><br>
  421. function my_callback(<span class="var">$element</span>) {<br>
  422. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">// Hide all &lt;b&gt; tags </span><br>
  423. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($element-&gt;tag==<span class="var">'b'</span>)<br>
  424. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$element-&gt;outertext = '';<br>
  425. } <br>
  426. <br>
  427. <span class="comment">// Register the callback function with it's <strong>function name</strong></span><br>
  428. $html-&gt;<strong>set_callback</strong>(<span class="var">'my_callback'</span>);<br>
  429. <br>
  430. <span class="comment">// Callback function will be invoked while dumping</span><br>
  431. echo $html;
  432. </div>
  433. </div>
  434. </div>
  435. <div><br>
  436. Author: S.C. Chen (me578022@gmail.com)<br>
  437. Original idea is from Jose Solorzano's <a href="http://php-html.sourceforge.net/">HTML Parser for PHP 4</a>. <br>
  438. Contributions by: Contributions by: Yousuke Kumakura, Vadim Voituk, Antcs<br>
  439. </div>
  440. </div>
  441. </body>
  442. </html>
  443. <!--$Rev: 165 $-->