How can I get paragraph tag inside a specific ID via DOMDocument()
?
For example, the HTML is this:
<div id='content'>
xxx yyyy zzzz
fffuuu uuuueee
xxx yyyy pppppp zzzz
<p>i need only this line</p>
</div>
I just want to take P
tag in content id DIV
...
Note 1: I get whole content of the DIV with:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$newcontent = $xpath->query("//*[@id='content']");
Note 2: Don’t say getElementsByTagName
; the HTML contains too many P
tags.
I dare a getElementsByTagName
:) - you dont need xpath
at all :
$doc = new DOMDocument();
@$doc->loadHTML($html);
$p=$doc->getElementById('content')->getElementsByTagName('p')->item(0);
echo $p->nodeValue;
outputs
i need only this line