views:

383

answers:

3

Hi, 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?

A: 

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();
Coronatus
Yes i know. My code is example. But i want to add html code in text node.
scopus
So? I just tested the code and it outputs what you are asking for.
Coronatus
Html code is dynamic so i have to add in text node.
scopus
I gave the you code that does what you asked for. Now you aren't making any sense and it sounds like you haven't even tested the code. Bye now.
Coronatus
Edited my question. I just want to DOMDocument don't convert htmlentities. Sorry my English is bad.
scopus
@scopus: what Coronatus had put is correct, can you explain how it's not what you are asking?
Anthony Forloney
+2  A: 

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(); 
VolkerK
A: 
mcgrailm