1

I use DOMDocument. My code here.

$dom = new DOMDocument('1.0', 'utf-8');
$textNode = $dom->createTextNode('<input type="text" name="lastName" />');
$dom->appendChild($textNode);
echo $dom->saveHTML();

Output:

&lt;input type="text" name="lastName" /&gt;

I want to this output

<input type="text" name="lastName" />

How can i do?

0

3 Answers 3

3

You need something that actually parses the xml data instead of treating it as "plain" text. DOMDocumentFragment and DOMDocumentFragment::appendXML() can do that.
E.g.

$doc = new DOMDocument;
$doc->loadhtml('<html><head><title>...</title></head>
  <body>
    <form method="post" action="lalala">
      <div id="formcontrols">
      </div>
      <div>
        <input type="submit" />
      </div>
    </form>
  </body>
</html>');

// find the parent node of the new xml fragement
$xpath = new DOMXPath($doc);
$parent = $xpath->query('//div[@id="formcontrols"]');
// should test this first
$parent = $parent->item(0);

$fragment = $doc->createDocumentFragment();
$fragment->appendXML('<input type="text" name="lastName" />');
$parent->appendChild($fragment);

echo $doc->savehtml(); 
Sign up to request clarification or add additional context in comments.

Comments

1

Change createTextNode with createElement. Text nodes are for text :)

$dom = new DOMDocument('1.0', 'utf-8');
$textNode = $dom->createElement('input');
$textNode->setAttribute('type', 'text');
$textNode->setAttribute('name', 'lastName');
$dom->appendChild($textNode);
echo $dom->saveHTML();

5 Comments

Yes i know. My code is example. But i want to add html code in text node.
So? I just tested the code and it outputs what you are asking for.
Edited my question. I just want to DOMDocument don't convert htmlentities. Sorry my English is bad.
@scopus: what Coronatus had put is correct, can you explain how it's not what you are asking?
plus 1 for opening my eyes with: "Text nodes are for text"
1

look at the documentation one of the first things is says

"A quick note to anyone who is using character entities (e.g. ©) in this, and finding them automatically escaped. The correct thing to do here is to use the createEntityReference method (e.g. createEntityReference("copy");), and then appendChild this entity between text nodes. "

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.