Nikul
Nikul

Reputation: 1025

How to get all p tags after particular class in dom

I have an html:

<p class="story-body__introduction">2013 yazındaki Gezi Parkı eylemlerinin başlarından itibaren çeşitli medya kurumları, gösterilerin arkasında Sırp gençlik örgütü Otpor'un olduğunu iddia etti.</p>
<p>Geçtiğimiz günlerde ise, "Emniyet Genel Müdürlüğü Kaçakçılık ve Organize Suçlarla Mücadele Daire Başkanlığı'nın Gezi Parkı eylemlerinin devam ettiği 15 Haziran 2013'te İstanbul Organize Suçlarla Şube Müdürlüğü'ne gönderdiği yazıda eylemlerle ilgili Otpor'u işaret ettiği" bildirildi.</p>
<p>Radikal.com.tr'de yer alan habere göre, "Bu yazı üzerine dönemin İstanbul Organize Suçlarla Şube Müdürü Nazmi Ardıç, İstanbul Cumhuriyet Başsavcılığı'na yazdığı yazıda ve Savcı Muammer Akkaş da İstanbul 1 No'lu Hakimliği'ne başvurarak çeşitli bilgiler istedi."</p>
<p>Yazıda "Türkiye'de Otpor / Canvas örgütü tarafından bir halk hareketi geliştirilmeye çalışıldığı ve Otpor lideri İvan Maroviç'in birçok kişiyi bu yönde eğittiği" bildiriliyor.</p>
<p>Maroviç'in bu kapsamda oyuncu Memet Ali Alabora'nın da aralarında bulunduğu isimlerle görüştüğü iddia ediliyor.</p>
<p>Otpor, Sırbistan'da 1998 - 2004 yılları arasında faaliyet gösteren, dönemin lideri Slobodan Miloseviç'in devrilmesine neden olan sokak hareketlerinin önemli bileşenlerinden bir gençlik hareketi.</p>

My goal is: I want to get all the p tags after first class= "story-body__introduction"

Code:

$storyBodyIntroduction = $html->find('p[class=story-body__introduction]', 0)->innertext();

How can I get those other <p>?

Upvotes: 2

Views: 1407

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173532

You can use XPath for this, which is part of the DOM extension and shipped with most PHP distributions.

$doc = new DOMDocument;
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);

$query = '//p[preceding-sibling::p[@class="story-body__introduction"]]';

foreach ($xpath->query($query) as $node) {
    echo $node->textContent, PHP_EOL;
}

It selects all paragraph elements that follow a sibling with the desired class. Note that a different query is necessary if the element has more than one class.

Upvotes: 3

Kevin
Kevin

Reputation: 41885

Yes, its quite possible, you could add a flag, after its found, get all the rest of <p> innertexts:

$found = false;
$text = array();
foreach($html->find('p') as $p) {
    if($p->class == 'story-body__introduction') {
        $found = true;
        continue;
    }

    if($found) {
        $text[] = $p->innertext;
    }
}

echo '<pre>';
print_r($text);

Sidenote: ->innertext is an attribute/property not a function innertext()

Supplemental info: This answer actually gets all paragraph elements in the document. If it turns out that you only need those elements which are siblings of that paragraph starting point, you could also check their parents if they are in the same level. Example:

$found = false;
$text = array();

$start_point = $html->find('p.story-body__introduction', 0);
foreach($html->find('p') as $p) {
    if($p->class == 'story-body__introduction') {
        $found = true;
        continue;
    }

    if($found && ($p->parent() == $start_point->parent()) ) {
        $text[] = $p->innertext;
    }
}

echo '<pre>';
print_r($text);

This answer will only get those siblings (on the same level) from the starting point.

Upvotes: 2

Related Questions