Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP简单HTML DOM解析器获取具有相同类的所有div的内容_Php_Dom_Simple Html Dom - Fatal编程技术网

使用PHP简单HTML DOM解析器获取具有相同类的所有div的内容

使用PHP简单HTML DOM解析器获取具有相同类的所有div的内容,php,dom,simple-html-dom,Php,Dom,Simple Html Dom,我不熟悉使用PHP进行HTML DOM解析,有一个页面中有不同的内容,但具有相同的“类”,当我尝试获取内容时,我能够获取最后一个div的内容,是否可能以某种方式获取具有相同类的div的所有内容,请您查看我的代码: <?php include(__DIR__."/simple_html_dom.php"); $html = file_get_html('http://campaignstudio.in/'); echo $x = $html->find('h2[

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

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

在示例代码中

echo $x = $html->find('h2[class="section-heading"]',1)->outertext; 
当您使用第二个参数1调用
find()
时,这将只返回1元素。如果你找到了所有的,你可以用它们做任何你需要的事情

$list = $html->find('h2[class="section-heading"]');
foreach ( $list as $item ) {
    echo $item->outertext . PHP_EOL;
}
我刚刚测试的完整代码是

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>
我们有你需要的!
随时为您服务
让我们联系吧!

感谢Nigel的宝贵意见,但正如您在本网站的源代码中看到的,添加代码时会返回这3类h2的两个内容。请你帮我解决我提到的疑问。提前感谢,我刚刚添加了完整的代码和3个类的输出。我不知道为什么在我的结尾没有给出你提到的输出。输出为您服务让我们联系!第一个缺少了一些东西。我猜这里没有办法分享屏幕截图,否则我会分享你的屏幕截图,它仍然提供三分之二的内容。先生,我发现最后两个只有class=“section heading”,而第一个有两个class=“section heading text white”,这是否引起了任何关注,如果可能的话,请提供您的意见。