gpt4 book ai didi

PHP XPath。如何返回带有html标签的字符串?

转载 作者:行者123 更新时间:2023-11-28 03:45:55 26 4
gpt4 key购买 nike

<?php
libxml_use_internal_errors(true);
$html = '
<html>
<body>
<div>
Message <b>bold</b>, <s>strike</s>
</div>
<div>
<span class="how">
<a href="link" title="text">Link</a>, <b> BOLD </b>
</span>
</div>
</body>
</html>
';
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->strictErrorChecking = false;
$dom->recover = true;
$dom->loadHTML($html);

$xpath = new DOMXPath($dom);
$messages = $xpath->query("//div");
foreach($messages as $message)
{
echo $message->nodeValue;
}

此代码返回不带 html 标记的“消息粗体、删除链接、粗体”...

我要输出如下代码:

Message <b>bold</b>, <s>strike</s>

<span class="how">
<a href="link" title="text">Link</a>, <b> BOLD </b>
</span>

你能帮帮我吗?

最佳答案

$dom = new DOMDocument;
foreach($messages as $message)
{
echo $dom->saveHTML($message);
}

使用saveHTML()

关于PHP XPath。如何返回带有html标签的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6177987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com