Search code examples
phphtmlescapinghtml-entitieshtmlspecialchars

How to read a PHP string as HTML


I have a PHP file I'm using to generate an HTML file. This is the process:

$document = new DomDocument;
$document->preserveWhiteSpace = false;
$document->validateOnParse = true;
$document->loadHTML(file_get_contents("http://www.example.com/base.html"));

$testNode = $document->createElement("div", "This is a <br> test");
$document->appendChild($testNode);

$document->saveHTMLFile("output.html");

This spits out an HTML file containing the following element:

<div>This is a &lt;br&gt; test</div>

That is, the <br> tags get converted to &lt;br&gt; in the actual HTML. I have tried all of these methods to unescape the string:

htmlspecialchars("This is a <br> test");

rawurldecode("This is a <br> test");

urldecode("This is a <br> test");

function decodeashtml($str) {
$str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str));
return html_entity_decode($str,null,'UTF-8');;
}
decodeashtml("This is a <br> test");

but they all produce:

This is a &lt;br&gt; test

What else can I do to get HTML tags to appear correctly as HTML?


Solution

  • So I found exactly what I'm looking for:

    $document = new DomDocument;
    $document->preserveWhiteSpace = false;
    $document->validateOnParse = true;
    $document->loadHTML(file_get_contents("http://www.example.com/base.html"));
    
    $newDiv = $document->createElement("div");
    $fragment = $document->createDocumentFragment();
    $fragment->appendXML("<p>I can write<br/>my HTML here.</p>");
    $newDiv->appendChild($fragment);
    
    $document->appendChild($newDiv);
    
    $document->saveHTMLFile("output.html");