Camen Design Forum

How to save div contetn to text file

append delete Kalimaseven

Whats the php code to automatically save specifig div or ul raw content from live site to text file?.

Reply RSS

Replies

append delete #1. Martijn

What exactly do you want to save? You can use `file_put_contents` [1] to store the contents of a variable in a file.

If you need to extract HTML from a live external website, you can use DOMDocument to read the HTML [2]:

%
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTMLFile('http://example.com/');
%

Then use XPath [3] to get whatever element you want:

%
$xpath = new DOMXPath($dom);
$element = $xpath->query('//div[@id="example"]')->item(0);
%

Now you can get the HTML of just that element into a variable [4] and save it to a file:

%
$html = $dom->saveHTML($element);
file_put_contents('myfile.html', $html);
%

Hope that gives you an idea of how things work!

[1]: https://secure.php.net/manual/en/function.file-put-contents.php
[2]: https://secure.php.net/manual/en/domdocument.loadhtmlfile.php
[3]: https://secure.php.net/manual/en/class.domxpath.php
[4]: https://secure.php.net/manual/en/domdocument.savehtml.php

Reply

(Leave this as-is, it’s a trap!)

There is no need to “register”, just enter the same name + password of your choice every time.

Pro tip: Use markup to add links, quotes and more.

Your friendly neighbourhood moderators: Kroc, Impressed, Martijn