Camen Design Forum

How to save div contetn to text file

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

Append

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

Only the original author or a moderator can append to this post.

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

Your friendly neighbourhood moderators: Kroc, Impressed, Martijn