使用php在html / xml标签内添加文本

时间:2014-09-02 23:17:55

标签: php html xml html-parsing

我正在尝试在html标记内添加文字:

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

现在我希望它出现在body标签的末尾:

$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替换中找到了类似的答案,但只有完全替换文本的解决方案。

谢谢!

1 个答案:

答案 0 :(得分:2)

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