I'm trying to output html with DOM and everything is working fine but now I need to also add some php code in the document.
I read that using DOMDocumentFragment will do the job but it does nothing my case. For example:
<?php
$dom = new DOMDocument('1.0');
$body = $dom->createElement("body");
$dom->appendChild($body);
$php = $dom->createDocumentFragment();
$php->appendXML('<?php echo "good"; ?>');
$body->appendChild($php);
echo $dom->saveXML();
?>
When I launch this in my browser the page is just blank. In the source code you can see the php code (which isn't desired) but it doesn't execute.
Any help would be greatly appreciated.
DOMDocumentFragment->appendXML('php code here');
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: DOMDocumentFragment->appendXML('php code here');
Your already within PHP tags, so you don't need to embed more tags to execute PHP. You just pass the string or variable or the function, i.e.,
Code: Select all
$php->appendXML('good');Re: DOMDocumentFragment->appendXML('php code here');
I understand that but I'm trying to get DOM to read the php from an file being loaded into the document.
The file that I am loading has some php in it but it is being ignored.
Code: Select all
$php = new DOMDocument('1.0');
$html = new DOMDocument('1.0');
$php->loadHTMLFile('file.php');
$body = $html->importNode($php->getElementsByTagName("body")->item(0);
$import = $html->importNode($body,true);
$html->appendChild($import);
echo $html->saveXML();