Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
Php how set data in string using html id

I have long html text. Pass it from js to php. I need change ID content with my text.

I tried did it like this:

$html = new DOMDocument();
$html->loadHTML($codeText);
$html->getElementById('second_head')->nodeValue = $leadAddressTemplate;
$html->getElementById('rechnung_div')->nodeValue = $rehnungTemplate;
$res = $html->saveHTML();

It works but have problem - my <> chars system replaces to &lt; and &gt; and system adds <html><body> to my text.

How can I fix it? Maybe there are some flags for it?

For example, input is :

<tr>
    <td colspan="2" class="invoice-products" width="100%">
        <div id="rechnung_div"></div>
    </td>
</tr>

And it try do it:

$html->getElementById('rechnung_div')->nodeValue = '<p>It is rechnung</p>';

As result i have this:

 <tr>
    <td colspan="2" class="invoice-products" width="100%">
        <div id="rechnung_div">&lt;p&gt;It is rechnung&lt;/p&gt;</div>
    </td>
</tr>

Now I can set for example

element:

    $appended = $html->createElement('p', 'It is rechnung');
    $html->getElementById('rechnung_div')->nodeValue = '';
    $rechnung_div->appendChild($appended);

But how insert table , for example like this?

   <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>&nbsp;</h1><p id="invoice_nummer"></p></td>
    </tr>
    </tbody>
</table>

I can't do it with appendChild() function

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Rolling together the notes from the comments:

$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();

Output:

<tr>
    <td colspan="2" class="invoice-products" width="100%">
        <div id="rechnung_div"><p>I am a template</p></div>
    </td>
</tr>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda