【问题标题】:How to add text in between html tags如何在html标签之间添加文本
【发布时间】:2022-07-06 01:59:05
【问题描述】:

我想在 php.ini 的 html 标签之间添加一个字符串。我正在使用 php 的 dom 文档类,您只能在 html 中添加字符串。这是我正在尝试完成的示例

<tag>example</tag><another>sometext</another>

我想在这两个标签之间添加一个字符串,让它看起来像

 <tag>example</tag>STRING<another>sometext</another> 

我希望能够分离这些标签,这样我就可以使用explode函数将html页面中的每个标签拆分为一个数组,然后遍历它们以供以后使用。

【问题讨论】:

    标签: php html string insert tags


    【解决方案1】:

    你可以在没有标签的情况下添加一个文本节点。

    $doc = new DOMDocument();
    
    $tag = $doc->createElement('tag');
    $doc->appendChild($tag);
    $tag->appendChild($doc->createTextNode('example'));
    
    $node = $doc->createTextNode('STRING');
    $doc->appendChild($node);
    
    $another = $doc->createElement('another');
    $doc->appendChild($another);
    $another->appendChild($doc->createTextNode('sometext'));
    
    echo $doc->saveHTML();
    

    会给

    <tag>example</tag>STRING<another>sometext</another>
    

    【讨论】:

      【解决方案2】:

      你需要连接php和html。 示例:

      <?php echo "<tag>example</tag>.'STRING'.<another>sometext</another> " ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-27
        • 1970-01-01
        • 2021-11-11
        • 2021-05-27
        • 1970-01-01
        • 1970-01-01
        • 2016-09-10
        • 1970-01-01
        相关资源
        最近更新 更多