PHP Simple HTML DOM Parser使用jQuery风格的选择器。
修改HTML元素:从documentation例子
// 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>
刮Slashdot的:
// Create DOM from URL
$html = file_get_html('http://slashdot.org/');
// Find all article blocks
foreach($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);
你真的在寻找CSS3和XPATH风格的选择器。 JQuery和其他JS库基于那些标准化的XML选择器。 – bucabay 2009-09-01 20:49:08
最新的jQuery for PHP是https://github.com/technosophos/querypath – Petah 2012-03-29 04:26:12