简体   繁体   English

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

[英]How to add text in between html tags

I want to add a string in between html tags in php.我想在 php.ini 的 html 标签之间添加一个字符串。 I'm using php's dom document class and you can only add strings inside html.我正在使用 php 的 dom 文档类,您只能在 html 中添加字符串。 Here is an example of what I am trying to accomplish这是我想要完成的一个例子

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

I want to add a string in-between these two tags so it should look like我想在这两个标签之间添加一个字符串,所以它应该看起来像

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

I want to be able to seperate these tags so I can use the explode function to split every tag in the html page to an array then traverse them for later use.我希望能够分离这些标签,这样我就可以使用explode函数将html页面中的每个标签拆分为一个数组,然后遍历它们以供以后使用。

You can add a textnode without being or having a tag.您可以在没有标签的情况下添加文本节点。

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

will give会给

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

You need to connect php and html.你需要连接php和html。 Exemple:示例:

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

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

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