3

How can I get content from the second <p> tag inside a div with ID mydiv using DOMDocument?

For example, my HTML might look like:

<div class='mydiv'>
<p><img src='xx.jpg'></p>
<p>i need here</p>
<p>lorem ipsum lorem ipsum</p>
</div>

I'm trying to extract the following text:

i need here

How can I do it?

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
ersanyus
  • 103
  • 2
  • 9

1 Answers1

3

Getting the contents from nth <p> tag:

Use DOMDocument::getElementsByTagName() to get all the <p> tags, and use item() to retrieve the node value of the second tag from the returned DOMNodeList:

$index = 2;

$dom = new DOMDocument;
$dom->loadHTML($html);
$tags = $dom->getElementsByTagName('p');
echo $tags->item(($index-1))->nodeValue; // to-do: check if that index exists

Getting the contents from nth<p> tag inside a div with given ID

If you want to retrieve the node value of a <p> tag inside a specific ID, then you can use an XPath expression instead of getElementsByTagName():

$index = 2;
$id    = 'mydiv'

$dom = new DOMDocument;
$dom->loadHTML($html);

$xpath = new DOMXPath($dom);
$tags = $xpath->query(
    sprintf('//div[@id="%s"]/p', $id)
);

Demo.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • @user3147942, That was not the question what you asked. However, to do that you need to pass `div` inside the `getElementsByTagName` as used by Amal. – Shankar Narayana Damodaran Jan 29 '14 at 12:26
  • 1
    @ShankarDamodaran: Hate when that happens! I've updated the question to include the new requirement though ;) – Amal Murali Jan 29 '14 at 12:43
  • may i use this to get the div via CLASS name? – ersanyus Jan 29 '14 at 12:45
  • @user3147942: Comments are not the place where you ask *NEW* questions. Please [ask a new question](http://stackoverflow.com/questions/ask) if you have a new question. That said, if you want to search for a specific `CLASS_NAME`, you can change the expression to: `'//div[contains(@class,"CLASS_NAME")]/p'` – Amal Murali Jan 29 '14 at 12:48