how to add php tags with the dom parser

前端 未结 1 1550
盖世英雄少女心
盖世英雄少女心 2021-01-13 06:16

I create some HTML Templates with php doms functionality, now i like to add some php tags into my template i.e.

$input = $this->dom->createElement(\'in         


        
相关标签:
1条回答
  • 2021-01-13 06:38

    You need a Processing Instruction

    $php = $dom->createProcessingInstruction('php', 'echo $foo->bar;');
    

    Full example:

    $dom = new DOMDocument;
    $dom->loadXml('<html><head><title>Test</title></head><body/></html>');
    $dom->getElementsByTagName('body')->item(0)->appendChild(
        $dom->createProcessingInstruction('php', 'echo $foo->bar;')
    );
    $dom->format = TRUE;
    echo $dom->saveXML();
    

    Result:

    <?xml version="1.0"?>
    <html>
      <head>
        <title>Test</title>
      </head>
      <body>
        <?php echo $foo->bar;?>
      </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题