MY ENGLISH IS NOT GOOD :")) PLEASE HELP ME THAT
<?php
$htmldiv= //it is html ,and it is not XML.
"<div class=\"MyClass\" id=\"MyID\">
ABC <hr>
<div></div>
</div>";
$html=
"<!DOCTYPE HTML>
<html>
<head>
<meta charset='utf-8'>
<title>TEST</title>
</head>
<body></body>
</html>";
$dom = new DOMDocument();
$dom->loadHTML($html);
$elementlist = $dom->getElementsByTagName("body");
$body=$elementlist->item(0);
$body->appendChild( $htmldiv ); // it is error. i want to append $htmldiv into $body and how can i do ????? :'(
echo $dom->saveHTML();
?>
I CAN NOT USE createDocumentFragment . HOW can i do??
$fragment = $dom->createDocumentFragment();
$fragment->appendXML($htmldiv);
$body->appendChild( $fragment ); // it is error.
I would suggest you to use PHP's Simple HTML DOM Parser and do it easily (just like jQuery)
include('simple_html_dom.php');
$html = str_get_html("<!DOCTYPE HTML><html><head><meta charset='utf-8'><title>TEST</title></head><body></body></html>");
$html->find('body', 0)->innertext = "<div class='MyClass' id='MyID'>ABC<hr><div></div></div>";
echo $html;
It's a small library to make the dom manipulation easy and it really did it, you can use it easily, it's a fun.