Tengo texto html largo. Pásalo de js a php. Necesito cambiar el contenido de ID con mi texto.
Intenté hacerlo así:
$html = new DOMDocument(); $html->loadHTML($codeText); $html->getElementById('second_head')->nodeValue = $leadAddressTemplate; $html->getElementById('rechnung_div')->nodeValue = $rehnungTemplate; $res = $html->saveHTML(); Funciona pero tiene un problema: mi sistema <> chars reemplaza a < y > y el sistema agrega <html><body> a mi texto.
¿Cómo puedo arreglarlo? Tal vez hay algunas banderas para ello?
Por ejemplo, la entrada es:
<tr> <td colspan="2" class="invoice-products" width="100%"> <div id="rechnung_div"></div> </td> </tr>Y trata de hacerlo:
$html->getElementById('rechnung_div')->nodeValue = '<p>It is rechnung</p>';Como resultado tengo esto:
<tr> <td colspan="2" class="invoice-products" width="100%"> <div id="rechnung_div"><p>It is rechnung</p></div> </td> </tr>Ahora puedo poner por ejemplo
elemento:
$appended = $html->createElement('p', 'It is rechnung'); $html->getElementById('rechnung_div')->nodeValue = ''; $rechnung_div->appendChild($appended);Pero, ¿cómo insertar una tabla, por ejemplo, así?
<table> <tbody><tr> <td style="width: 40%;vertical-align: baseline;"><h1 id="invoice_type">Rechnung</h1> <p id="invoice_title">gtrgtrgrtgtr</p></td> <td style="width: 60%;text-align: right;vertical-align: bottom;"><h1> </h1><p id="invoice_nummer"></p></td> </tr> </tbody> </table>No puedo hacerlo con la función appendChild()
Juntando las notas de los comentarios:
$parent = <<<_E_ <tr> <td colspan="2" class="invoice-products" width="100%"> <div id="rechnung_div"></div> </td> </tr> _E_; $template = <<<_E_ <p>I am a template</p> _E_; $temp = new DOMDocument(); $temp->loadHTML($template, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); // get the document's root element $temp_root = $temp->documentElement; $html = new DOMDocument(); $html->loadHTML($parent, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); // import the template into the current document $node = $html->importNode($temp_root, true); $html->getElementById('rechnung_div')->appendChild($node); echo $html->saveHTML();Producción:
<tr> <td colspan="2" class="invoice-products" width="100%"> <div id="rechnung_div"><p>I am a template</p></div> </td> </tr>