使用PHP Simple HTML DOM解析器获取同一类的所有div的内容 [英] Fetch content of all div with same class using PHP Simple HTML DOM Parser

查看:44
本文介绍了使用PHP Simple HTML DOM解析器获取同一类的所有div的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉使用PHP进行HTML DOM解析,有一个页面的内容不同但具有相同的类",当我尝试获取内容时,我能够获取上一个div的内容,是吗可能以某种方式我可以获取具有相同类的div的所有内容,请您仔细查看我的代码:

I am new to HTML DOM parsing with PHP, there is one page which is having different content in its but having same 'class', when I am trying to fetch content I am able to get content of last div, Is it possible that somehow I could get all the content of divs having same class request you to please have a look over my code:

<?php
    include(__DIR__."/simple_html_dom.php");
    $html = file_get_html('http://campaignstudio.in/');
    echo $x = $html->find('h2[class="section-heading"]',1)->outertext; 
?>

推荐答案

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

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>

这篇关于使用PHP Simple HTML DOM解析器获取同一类的所有div的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆