OK , here is a html content that i have :
<p> .... </p>
<div id="quick_preview">
<p>the contents </p>
</div>
and what i want is :
<p> .... </p>
<div id="quick_preview">
<description>
<p>the contents </p>
</description>
</div>
i use this routine to find the div and create description tag, but i don’t know how to insert <description> into div before p:
$dom = new DOMDocument;
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
$dom->loadHTML($row['body']);
$divs = $dom->getElementsByTagName('div');
foreach($divs as $div)
{
if ($div->getAttribute( "id" ) == 'quick_preview') {
$desc_element = $dom->createElement('description');
}
}
$dom->saveHTMLFile($html)
This will do what you want:
You could drop the XPath and use
$dom->getElementById('quick_preview')instead.Note that you can move DOM nodes by simply appending them somewhere else in the document.