PHP XPath。如何使用html标签返回字符串?

时间:2011-05-30 15:12:26

标签: php html xpath domdocument domxpath

<?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>

你能帮助我吗?

2 个答案:

答案 0 :(得分:0)

我可以使用SimpleXML非常快速地完成它(如果你可以从DOMDocument和DOMXPath切换,可能你会选择我的解决方案):

$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>
    ';
    $xml = simplexml_load_string($html);
    $arr = $xml->xpath('//div/*');
    foreach ($arr as $x) {
      echo $x->asXML();
    }

答案 1 :(得分:0)

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

使用saveHTML()