here is a code:
<?php
$html = <<< HTML
<div id="one">
<h1>header 1</h1>
<h2>header 2</h2>
<blockquote>
<p>paragraph1</p>
<p>paragraph2</p>
</blockquote>
<b>bold text1</b>
<b>bold text2</b>
</div>
HTML;
$dom = new DOMDocument();
@$dom->loadHTML($html);
/******************3rd part*************/
echo $dom->childNodes->item(0)->nodeName."<br>";
echo $dom->childNodes->item(1)->nodeName."<br>";
/**********1st part**********/
$tags = $dom->getElementsByTagName("blockquote");
foreach($tags as $tag)
{
$ps=$tag->getElementsByTagName("p");
foreach($ps as $p)
{
echo $p->nodeValue."<br>";
}
}
/************2nd part**********/
$tags = $dom->getElementById("one");
foreach($tags as $tag)
{
$hs=$tag->getElementsByTagName("h1");
foreach($hs as $h)
{
echo $h->nodeValue."<br>";
}
}
?>
Please teach me by example :
- What is the firstNode and lastNode in $html? How can I print the text inside those nodes?
- Why the second part of the code prints nothing ?
<h2>header 2</h2>
is this a single node ?- What is the difference between textContet and nodeValue ?
- What is the idea of textContent, item() and childnodes?
- How many child odes does div#one contain?
- Which are the childNodes of $dom? How can I print their names?
- What is the error in the 3rd part of this code? Both lines are showing the same!
I have read @Gordon's answer from here but I need some clear examples.