gpt4 book ai didi

php - 使用 Simple HTML DOM 查找具有最多

的 DIV

转载 作者:行者123 更新时间:2023-11-28 02:25:06 25 4
gpt4 key购买 nike

我正在尝试为一个元素使用简单的 HTML DOM 解析器 (PHP)。基本上,我有这样一个网页的源代码-

<html>
...
...
<div id="1> Some content here</div>
<div id="2> Some content here</div>
<div id="3> Some content here</div>
<div id="4> Some content here</div>
....
...
</html>

这个 div 中的一些包含段落 (<p>) ,现在我想打印包含大部分 <p> 的 div 段落里面的标签。这是我第一次使用 HTML 解析器,我尝试了很多东西,但没有任何东西给我合适的输出。编辑-一个 div 可能有一些父 div。所以我想打印包含最多 <p> 的 div 文本.对于下面的例子,我只想要 div id 4 的输出:

<html>
...
...
<div id="1> <p> Hey</p></div>
<div id="2> <p> Hey there!</p></div>
<div id="3>
<p> How are you<p>
<div id="4">
<p> Hey</p>
<p> How are you</p>
</div>
</div>

....
...
</html>

最佳答案

    $html = file_get_html('http://your_web_page');

$arr = array();
// Find all divs, but you should include the parent class
foreach($html->find('.parent_class_name div') as $key => $element) {
echo $key." : ".count($element->find('p')) . '<br>';
$arr[$key] = count($element->find('p'));
}
print_r(max($arr));

关于php - 使用 Simple HTML DOM 查找具有最多 <p> 的 DIV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47995028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com