使用php在html/xml标记中添加文本


Add text inside html/xml tag with php

我正在尝试在html标签中添加文本:

$html = '<html>
<head>
   <title>title</title>
</head>
<body style="background-color:yellow;">
   <p>some text</p>
</body>
</html>';

现在我希望这个出现在正文标签的末尾:

$replacement = '<footer>Footer Text</footer>';

所以结果看起来是这样的:

<html>
<head>
   <title>title</title>
</head>
<body style="background-color:yellow;">
   <p>some text</p>
   <footer>Footer Text</footer>
</body>
</html>

我用preg或ereg替换来寻找类似的答案,但只有在文本被完全替换的情况下才能找到解决方案。

谢谢!

$new_html= str_replace("</body>", "<footer>Footer Text</footer></body>", $html);