(2)解压,manual目录是使用文档(很容易看懂的,也可以看这里),example目录是一些实例,可以参考使用;
include "simple_html_dom.php" ; // Create DOM from URL or file$html = file_get_html('http://www.google.com/');// Find all images foreach($html->find('img') as $element) echo $element->src . '<br>';// Find all links foreach($html->find('a') as $element) echo $element->href . '<br>';
// Create DOM from URL$html = file_get_html('http://slashdot.org/');// Find all article blocksforeach($html->find('div.article') as $article) { $item['title'] = $article->find('div.title', 0)->plaintext; $item['intro'] = $article->find('div.intro', 0)->plaintext; $item['details'] = $article->find('div.details', 0)->plaintext; $articles[] = $item;}print_r($articles);
// Create DOM from string$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>'); $html->find('div', 1)->class = 'bar';$html->find('div[id=hello]', 0)->innertext = 'foo';echo $html; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>
Name | Description |
---|---|
void __construct ( [string $filename] ) | Constructor, set the filename parameter will automatically load the contents, either text or file/url. |
string plaintext | Returns the contents extracted from HTML. |
void clear () | Clean up memory. |
void load ( string $content ) | Load contents from a string. |
string save ( [string $filename] ) | Dumps the internal DOM tree back into a string. If the $filename is set, result string will save to file. |
void load_file ( string $filename ) | Load contents from a from a file or a URL. |
void set_callback ( string $function_name ) | Set a callback function. |
mixed find ( string $selector [, int $index] ) | Find elements by the CSS selector. Returns the Nth element object if index is set, otherwise return an array of object. |
Attribute Name | Usage |
---|---|
$e->tag | Read or write the tag name of element. |
$e->outertext | Read or write the outer HTML text of element. |
$e->innertext | Read or write the inner HTML text of element. |
$e->plaintext | Read or write the plain text of element. |
Method | Description |
---|---|
mixed $e->children ( [int $index] ) | Returns the Nth child object if index is set, otherwise return an array of children. |
element $e->parent () | Returns the parent of element. |
element $e->first_child () | Returns the first child of element, or null if not found. |
element $e->last_child () | Returns the last child of element, or null if not found. |
element $e->next_sibling () | Returns the next sibling of element, or null if not found. |
element $e->prev_sibling () | Returns the previous sibling of element, or null if not found. |