我正在尝试将Simple HTML DOM解析器(PHP)用于项目。基本上,我有一个像这样的网页的源代码 -
chown
这个div中的一些包含段落~/.config
,现在我想要打印div中包含大部分<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>
标记的段落。这是我第一次使用HTML解析器,我尝试了很多东西,但没有给我适当的输出。
编辑 - 一个div可能有一些父div。所以我想打印包含最多(<p>)
的div的文本。对于下面的示例,我只想要div id 4的输出:
<p>
答案 0 :(得分:0)
$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));
答案 1 :(得分:0)
执行此操作(运行代码段并单击按钮以获取结果):
$("#clickMe").on('click', function()
{
var mostP = 0;
var textOfMostP = '';
// iterate through all divs on page
$( "div" ).each(function()
{
// check the count of childern p elements inside current div
if($(this).children("p").length > mostP)
{
textOfMostP = this.innerHTML;
mostP = $(this).children("p").length;
}
});
// If a result for most p's has been found
if(mostP != 0)
{
$( "#result" ).html("Result:<br/>Number of p's: " + mostP + "<br/><br/>The text:" + textOfMostP);
}
});
&#13;
div
{
position: relative;
float:left;
border:1px solid #09f;
border-radius:4px;
margin-bottom: 10px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<p>this is some text</p>
<p>this is some more</p>
<p>this is even more</p>
</div>
<div>
<p>there is only this here</p>
</div>
<div>
<p>two lines are here</p>
<p>this is second line</p>
</div>
<input type="button" id="clickMe" value="Write div with most <p>s" />
<div id="result">The result will come here</div>
&#13;