如何在html标签之间添加文本



我想在php的html标签之间添加一个字符串。我使用php的dom文档类,你只能在html中添加字符串。这里是一个例子,我试图完成

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

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

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

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

您可以添加textnode而不需要标记。

$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>

你需要连接php和html。例:

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