1

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?

11
  • You could str_replace it? Make an array of all the values you need. Same principle as bbcode Commented Sep 23, 2015 at 13:27
  • What is $document object? Commented Sep 23, 2015 at 13:30
  • @Inurosen $document = new DomDocument; Commented Sep 23, 2015 at 13:33
  • Is there any particular reason you're using DomDocument? Commented Sep 23, 2015 at 13:38
  • 1
    I see why you use it now. The solution offered by ceejayoz is probably the best you'll get. Commented Sep 23, 2015 at 15:27

3 Answers 3

1

you can try this:

<?php echo html_entity_decode("this is <br> test."); ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Because I don't want to output the text directly on the page, but rather want to store it in a variable, I don't think I can use echo.
so you can also store this in a php variable, and whenever you want to use then you can.
1

<p>This is a <br /> test</p> is a p element containing a text node, a br element, and another text node.

To do this properly with PHP's XML writer:

$element = $document->createElement('p');

$element->appendChild($document->createTextNode('This is a '));
$element->appendChild($document->createElement('br'));
$element->appendChild($document->createTextNode(' test'));

$document->appendChild($element);

1 Comment

This works, thank you. It's a little more verbose than I was hoping for (I'd hoped to be able to use HEREDOC for multi-line HTML text), so before I accept this as the right answer, I want to check if there's a way to do that instead.
1

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.