How to extract innerHTML using PHP Dom

I am currently using nodeValue to give me HTML output, however it strips out the HTML and just gives me plain text. Does anyone know how I can modify my code to give me the inner HTML of an element using its ID?

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;


}

      

+3


source to share


2 answers


This works for me:

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

      

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



If you have PHP 5.3.6 this might also be an option:

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

      

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

+2


source


I change the code and it works great for me. Below is the code



    $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;

      

0


source







All Articles