我纠正你的HTML例子,除非你会说,它不是一个错误!
在此基础上,我们有:
只有一个img标签,所以我们可以直接搜索并拿到冠军$table->find('img',0)->title
其他所有想文本withing一个div标签,因此我们可以搜索所有div,然后使用循环打印其内容。
这是一个工作
$table = '
|
TEXT WHICH I NEED 2nd TEXT WHICH I NEED 3rd TEXT WHICH I NEED 4th TEXT WHICH I NEED HREF which I need TEXT which I need also |
//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load($table);
/
// Find img title
$imgTitle = $html->find('img',0)->title;
echo "IMG title: $imgTitle ";
// Find all divs
$divs = $html->find('div');
echo "DIV's content:";
// loop through all found divs and print their content
foreach($divs as $i => $div) {
echo "$i: " . $div->plaintext . "
";
}
// Clear DOM object
$html->clear();
unset($html);
输出
IMG title:
this is img which I need also
DIV's content:
0: TEXT WHICH I NEED
1: 2nd TEXT WHICH I NEED
2: 3rd TEXT WHICH I NEED
3: 4th TEXT WHICH I NEED
4: HREF which I need
5: TEXT which I need also