ali raha
ali raha

Reputation: 221

PHP Simple DOM Parser first child

this is a sample code:

<div class="content">
<p>text 1</p>
<p>text 2</p>
<p>text 3</p>
<table border="1" cellpadding="0" cellspacing="0" dir="ltr">
    <tbody>
        <tr>
            <td valign="top" width="205">
                <p>td1</p>
            </td>
            <td valign="top" width="205">
                <p>td2</p>
            </td>
            <td valign="top" width="205">
                <p>td3</p>
            </td>
        </tr>
    </tbody>
</table>
</div>

I want print just first level p paragraph

So I try this code:

foreach($html->find('div.content p') as $p)
{
    echo $p->plaintext;
    echo "<br/>";
}

I expect these result:

text 1
text 2
text 3

But I get these:

text 1
text 2
text 3
td1
td2
td3

Is there any way to ignore other p tags?

Upvotes: 1

Views: 1348

Answers (1)

pavel
pavel

Reputation: 27082

If the selectors are the same as in CSS, the selector you are looking for is

foreach($html->find('div.content > p') as $p) {
    //                          ^^
    ...
}

Using this selectors you find direct children, yours .content p find all p in .content, not just direct children.

Upvotes: 2

Related Questions