0

I am using $doc = new DOMDocument();

I just want to get HTML from a particular id rather than the whole HTML.

I tried

$file=file_get_contents($url);
    $doc = new DOMDocument();
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput       = true;
    $doc->load($file);
    $content = $doc->getElementById("site-content");
    echo $doc->saveHTML($content);

But it's not working.

Harsha pps
  • 2,012
  • 2
  • 25
  • 35
Jasbir Rana
  • 259
  • 3
  • 13

1 Answers1

1
<?php
    $file=file_get_contents("https://www.wikipedia.org/");
    $doc = new DOMDocument();
    $doc->loadHtml($file);
    $content = $doc->getElementById("searchInput");
    echo $doc->saveHTML($content);
Ganesan San
  • 174
  • 6