Question

i am working on PHP, and i've only just begun, so i would like to ask some advice on something i can't quite seem to find online.

I have a PHP-file that gets 2 strings: A name and a lot of HTML-text.

$name = $_POST['name'];
$content = $_POST['content'];

Now i want to use those 2 to create a new HTML file and save it. So far i've managed to make it save a new HTML file and use the name as the <title> tag. Now what i want is to do is replace the (currently empty) body with my HTML string, interpreted as HTML.

This builds my body tag:

$body = $doc->createElement('body');
$body = $root->appendChild($body);

Now i found 2 ways to do this, and both don't work:

Solution 1:

$content = $doc->createTextNode($content);
$content = $body->appendChild($content);

This inserts my HTML into my body, but it parses literally as '<div id="lala">content</div>'. So this isn't what i want.

Solution 2:

$content = $doc->loadHTML($content);

This actually makes the html load as HTML, but now it replaces my entire HTML and things like adding css and js to the head will now actually go UNDER the new body, instead of in the head. As such:

$link_1 = $doc->createElement('link');
$link_1->setAttribute('rel','stylesheet');
$link_1->setAttribute('href','css/mystylesheet.min.css');
$link_1 = $head->appendChild($link_1);

So basically i want to both interpret my string as HTML, AND make it load in the correct place. I've tried to change it to $body->loadHTML($content), but this gives me an internal error.

Does anyone know the PHP-method i'm looking for? Thanks!

Was it helpful?

Solution

This will work. Let me know if you need explanation :)

<?php
ini_set( 'display_errors', 1 );
ini_set( 'error_reporting', E_ALL );

$dom = new DOMDocument( '1.0' );
$dom->formatOutput = true;
$dom->preserveWhiteSpace = true;

// test values
$name = 'Test';
$content = '<div onclick="alert(\'Hello!\');">Test div</div>';

// create <html>, <head>, <title> and <body> tags
$html = $dom->createElement( 'html' );
$head = $dom->createElement( 'head' );
$title = $dom->createElement( 'title' );
$body = $dom->createElement( 'body' );

// title text
$titleText = $dom->createTextNode( $name );

// import the text in a new dom
$dom1 = new DOMDocument( '1.0' );
$dom1->formatOutput = true;
$dom1->preserveWhiteSpace = true;
$bodyText = $dom1->loadHTML( $content );
$bodyText = $dom1->getElementsByTagName('body')->item(0);

// add them to the dom
$html = $dom->appendChild( $html );

$html->appendChild( $head );
$head->appendChild( $title );
$title->appendChild( $titleText );

$html->appendChild( $body );
$bodyT = $dom->importNode( $bodyText, true );
$body->appendChild( $bodyT );
echo $dom->saveHTML();
?>

Hope this helps.

OTHER TIPS

Of course, the text in createTextNode stands for plain text (vs. HTML).

Fatal error: Call to undefined method DOMElement::loadHTML()

Correct, loadHTML() belongs to DOMDocument, not DOMElement. In your case, the document is apparently $doc (not $body). So you need to call:

$doc->loadHTML()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top