3

我目前正在使用 nodeValue 来给我 HTML 输出,但是它正在剥离 HTML 代码并只给我纯文本。有谁知道我如何修改我的代码以通过使用它的 ID 为我提供元素的内部 HTML?

function getContent($url, $id){

// This first section gets the HTML stuff using a URL
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$html = curl_exec($ch);
curl_close($ch);

// This second section analyses the HTML and outputs it
$newDom = new domDocument;
$newDom->loadHTML($html);
$newDom->preserveWhiteSpace = false;
$newDom->validateOnParse = true;

$sections = $newDom->getElementById($id)->nodeValue;
echo $sections;


}
4

2 回答 2

2

这对我有用:

$sections = $newDom->saveXML($newDom->getElementById($id));

http://www.php.net/manual/en/domdocument.savexml.php

如果您有 PHP 5.3.6,这也可能是一个选项:

$sections = $newDom->saveHTML($newDom->getElementById($id));

http://www.php.net/manual/en/domdocument.savehtml.php

于 2012-03-09T14:03:03.203 回答
0

我已经修改了代码,它对我来说工作正常。请在下面找到代码

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    $html = curl_exec($ch);
    curl_close($ch);
    $newDom = new domDocument;
    libxml_use_internal_errors(true);
    $newDom->loadHTML($html);
    libxml_use_internal_errors(false);
    $newDom->preserveWhiteSpace = false;
    $newDom->validateOnParse = true;

    $sections = $newDom->saveHTML($newDom->getElementById('colophon'));   
    echo $sections;
于 2014-04-20T04:22:22.563 回答