如何使用PHP Dom提取innerHTML

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;


}

现在这段代码是做什么的?($sections 里面有什么内容?) - enygma
你尝试过使用 $sections = $newDom->getElementById($id)->saveHTML(); 吗? - Martin
$newDom->saveHTML($newDom->getElementById($id)); http://ru2.php.net/manual/zh/domdocument.savehtml.php - kirilloid
2个回答

2

嘿,马丁,太棒了。运行正常。 - user1259294

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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接