PHP Simple HTML DOM Parser(PHP实现的简单的HTML DOM解析器)。
Description, Requirement & Features
- A HTML DOM parser written in PHP5+ let you manipulate HTML in a very easy way!
- Require PHP 5+.
- Supports invalid HTML.
- Find tags on an HTML page with selectors just like jQuery.
- Extract contents from HTML in a single line.
Download & Documents
Quick Start
- How to get HTML elements?
1 2 3 4 5 6 7 8 9 10 11
| $html = <strong>file_get_html</strong>(‘http:
foreach($html-><strong>find</strong>(‘img’) as $element) echo $element-><strong>src</strong> . ‘<br>’;
foreach($html-><strong>find</strong>(‘a’) as $element) echo $element-><strong>href</strong> . ‘<br>’;
|
- How to modify HTML elements?
1 2 3 4 5 6 7 8 9
| $html = <strong>str_get_html</strong>(‘<div id="hello">Hello</div><div id="world">World</div>’);
$html-><strong>find</strong>(‘div’, 1)-><strong>class</strong> = ‘bar’;
$html-><strong>find</strong>(‘div[id=hello]’, 0)-><strong>innertext</strong> = ‘foo’;
echo $html; // Output: <div id="hello"><strong>foo</strong></div><div id="world" <strong>class="bar"</strong>>World</div>
|
- Extract contents from HTML
1 2 3
| echo <strong>file_get_html</strong>(‘http:
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| $html = <strong>file_get_html</strong>(‘http:
foreach($html-><strong>find</strong>(‘div.article’) as $article) { $item[‘title’] = $article-><strong>find</strong>(‘div.title’, 0)-><strong>plaintext</strong>; $item[‘intro’] = $article-><strong>find</strong>(‘div.intro’, 0)-><strong>plaintext</strong>; $item[‘details’] = $article-><strong>find</strong>(‘div.details’, 0)-><strong>plaintext</strong>; $articles[] = $item; }
print_r($articles);
|