且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PHP Simple HTML DOM解析器获取同一类的所有div的内容

更新时间:2023-09-03 17:50:28

在您的示例代码中,您已经

In your example code, you have

echo $x = $html->find('h2[class="section-heading"]',1)->outertext; 

当您使用第二个参数1调用 find()时,这只会返回1元素.相反,如果找到了所有这些对象,那么您可以对它们进行任何操作...

as you are calling find() with a second parameter of 1, this will only return the 1 element. If instead you find all of them - you can do whatever you need with them...

$list = $html->find('h2[class="section-heading"]');
foreach ( $list as $item ) {
    echo $item->outertext . PHP_EOL;
}

我刚刚测试过的完整代码是...

The full code I've just tested is...

include(__DIR__."/simple_html_dom.php");
$html = file_get_html('http://campaignstudio.in/');

$list = $html->find('h2[class="section-heading"]');
foreach ( $list as $item ) {
    echo $item->outertext . PHP_EOL;
}

给出输出...

<h2 class="section-heading text-white">We've got what you need!</h2>
<h2 class="section-heading">At Your Service</h2>
<h2 class="section-heading">Let's Get In Touch!</h2>