簡體   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