[英]php simple dom parser get parent of element
我正在嘗試在沒有區分類別或父類別的某些DIV上檢索img SRC:它們僅存在於<div class="content_end"></div>
我需要從這些div's
檢索圖像scr
,如何僅在頁面上僅檢索這些div而不檢索所有其他div:
我試圖用div作為父對象來查找所有圖像,但這沒有用:
foreach($domParse->find('img') as $element)
echo $element->parent('div')->src;
我知道在jquery中有一個nextAll選擇器,因此我可以執行以下操作:
$('.content_end').nextAll('div');
這向我返回了我需要的內容:如何在Simple Dom Parser中做到這一點:
<div class="content_end"> </div>
-------------------------------------------------
<div style="postion:relative; height:90px;">
<img src="../">
</div>
<div style="postion:relative; height:90px;">
<img src="../">
</div>
---------------------------------------------
我認為您正在尋找的是next_sibling()
:
foreach($domParse->find('div.content_end') as $element)
echo $element->next_sibling()->find("img")[0]->src;
由於您的目標<img>
標記的父div 沒有類或id,因此只需將屬性過濾器與父!class
屬性過濾器一起使用即可 。 考慮以下示例:
include 'simple_html_dom.php';
$html_string = '
<div class="content_end"></div>
<div style="postion:relative; height:90px;">
<img src="http://www.google.com/image1.jpg" />
</div>
<div style="postion:relative; height:90px;">
<img src="http://www.stackoverflow.com/image2.png" />
</div>'; // your sample html structure
$domParse = str_get_html($html_string);
// div which has no class with img tag children
foreach($domParse->find('div[!class] img') as $img) {
echo $img->src . '<br/>';
}
應按示例使用:
http://www.google.com/image1.jpg
http://www.stackoverflow.com/image2.png
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.