0

I would like to know if it is possible to save only a part of a web page as HTML after it is finished rendering.

Let's say I have a web page, on this web page there is a tile view. This tile view is a UL list with an ID.

After this finishes loading, I would like to save away only this UL list as an HTML file, so that the next time I load the page, under defined circumstances, I load/output this HTML file and don't have to do the "tiling" functions.

I have already found this, how to generally save a page as HTML https://stackoverflow.com/a/3775302/11813184 But unfortunately nothing about whether I can also save away only a certain area, such as a UL list with a defined ID.

enky
  • 40
  • 8
  • You could capture the whole page, with DOM manipulation (I would suggest using [PHP Simple HTML DOM Parser](https://simplehtmldom.sourceforge.io), if you can, it makes your life quite simple) keep only the part you want and then load that. – Ant Jul 16 '21 at 10:40

2 Answers2

1

Try to use SimpleHTMLDom library for your needs - https://simplehtmldom.sourceforge.io/manual.htm

This is like jQuery, but for PHP.

$html = file_get_html('http://www.google.com/');

// Find all anchors, returns a array of element objects
$ret = $html->find('ul');

// Find (N)th anchor, returns element object or null if not found (zero based)
$ret = $html->find('a', 0);

// Find lastest anchor, returns element object or null if not found (zero based)
$ret = $html->find('a', -1);

// Find all <div> with the id attribute
$ret = $html->find('div[id]');

// Find all <div> which attribute id=foo
$ret = $html->find('div[id=foo]');
Alexander Z
  • 630
  • 3
  • 22
1

I have used successfully ob_start and ob_get_contents as they are described here. Save current page as HTML to server