繁体   English   中英

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

[英]How to add text in between html tags

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

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM