如何将简单的html(从其他文件)附加到PHP中的HTML DOM元素?

I have the main document created with DOM server-side using PHP. And I have to include some pieces of PHP/HTML without DOM. Example. There is file form.php containing a form

<form action="" method="post"> 
  <input type="text" name="firstname" id="firstnameid" /><br/>
  <input type="submit" name="submit" value="Submit" />
</form>

I want to place that form into a <DIV> created using DOM in the file index.php. The DIV inside depends on other logic (condition $form_is_needed)

<!DOCTYPE html>
<html lang="en">
<head> <meta charset="utf-8" /> </head>

<body>
<?php
$form_is_needed = true;
$dom = new DOMDocument();
$domDiv = $dom->createElement("div"); 

if ($form_is_needed) {
  // trying load not DOM
  $str = file_get_contents('form.php');
  $domDivText = $dom->createTextNode( html_entity_decode( $str )  );
  $domDiv->appendChild($domDivText);
}
else {
  $domDivText = $dom->createTextNode( "There is no form." );
  $domDiv->appendChild($domDivText);
}

$dom->appendChild($domDiv);
$html = $dom->saveHTML();
echo $html; 
?>
</body>
</html>

I get my content inside DIV but the escape characters has been added

<div>&lt;form action="" method="post"&gt; 
  &lt;input type="text" name="firstname" id="firstnameid" /&gt;&lt;br/&gt;
  &lt;input type="submit" name="submit" value="Submit" /&gt;
&lt;/form&gt;
</div>

There is, to my knowledge, no node which can emit raw content, but if it is well-formed, you could try loading it into a DOMDocumentFragment and appending it that way:

$frag = $dom->createDocumentFragment();
$frag->appendXML($str);
$domDiv->appendChild($frag);

Replace

$str = file_get_contents('form.php');

With

$str = readfile('form.php');

That should do it.