Hi everyone, I need help with creating a function to download a file by getting the text or content inside the specific DIV.
Thanks is advance.
Hi everyone, I need help with creating a function to download a file by getting the text or content inside the specific DIV.
Thanks is advance.
Take a look at Simple HTML DOM Parser
Hi, thanks for the reply. I am actually new to this. So i made a test page. I copy and paste this code:
// Create DOM from string
$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');
$html->find('div', 1)->class = 'bar';
$html->find('div[id=hello]', 0)->innertext = 'foo';
echo $html; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>
and I’m getting an error:
Fatal error: Call to undefined function file_get_html() in /home/edber/public_html/schema/download.php on line 33
can you tell me what’s wrong?
Did you include the library itself?
Like so
include('path/to/simple_html_dom.php');
I forgot to include that. Thanks.
what does this mean? $html = file_get_html('http://www.google.com/');
do I need to replace this with my own domain?
This function reads HTML from specified URL (or file) and returns an object you can manipulate with.
I’m now getting it little by little. So I test it again if I’l be able to get the content of a specfied DIV.
<?php
include('simple_html_dom.php');
$html = file_get_html('http://schema.e-db.info/download.php');
$code = $html->find('#test', 0);
var_dump($code->outertext);
?>
and then execute:
<?php
echo $code;
?>
Then this was the output:
string(39) "
This is my content
"
Why does it shows string(39)
? I want it just like: > This is my content
You’re using var_dump() function. It shows not only the value of variable but also its type and size.
Use regular echo
or print
instead.
This is what you want:
include('simple_html_dom.php');
$html = file_get_html('http://schema.e-db.info/download.php');
$code = $html->find('#test', 0);
echo $code->outertext;
Hi, yes that is exactly what I want. Thank you so much. Now I’m looking for a way of how will I able to download that content as a txt file.
Do you have any idea of achieving it?
I got this through my research, but trying to figure out how to trigger the download button:
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
readfile($filename);
Another update. This seems close to my question.
$filename = "yourfile.txt";
#...
$f = fopen($filename, 'w');
fwrite($f, $content);
fclose($f);
When I reload my page it automatically download a text file. When I open it. It has the html, php code that I created. How can create a Variable that will tell the code to get the specific content that I want?
I think we need a better description of what you’re trying to do.
You’ve figured out how to find the contents of the div (post #8, $code->outertext).
You then gave code to prompt a download to an end-user.
You then gave code to write a file on your server.
Now that you’ve gotten the content of the div, what are you trying to do with it?
Hi, what I\m trying to do is once I got the content of the DIV I want. I will be able to download it as a text file. That’s why I am looking for a code that will let me donwload a file as TXT. And the content of that TXt file is the content of the DIV that I chose. Ex:
I have
<div id="content">This is a content</div>
Then a simple DOM parser:
$html = file_get_html('www.mydomain.com');
$code = $html->find('#content');
echo $code;
technically it will output the same DIV right. So once I got that I want to download it as a txt file like this:
<a href="<?php echo $code; ?>"Download Text</a>
.
Hope you got it clearly. I really need this. Thanks.
Any help?
// parse div content
include('simple_html_dom.php');
$html = file_get_html('http://schema.e-db.info/download.php');
$code = $html->find('#test', 0);
$content = $code->outertext;
// store it to file
$filename = 'myfile.txt';
file_put_contents($filename, $content);
// send proper headers to force downloading by browser
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($filename));
header('Content-Disposition: attachment; filename="' . $filename . '"');
readfile($filename);
Actually, you don’t even have to save content to the file. You can send it directly to the browser. This is a shorter version of the code:
// parse div content
include('simple_html_dom.php');
$html = file_get_html('http://schema.e-db.info/download.php');
$code = $html->find('#test', 0);
$content = $code->outertext;
// send content to browser as a file
header("Content-Type: application/force-download");
header("Content-Length: " . sizeof($content));
header('Content-Disposition: attachment; filename=myfile.txt'); // myfile.txt here is virtual filename
echo $content;
Hi, this actually works. However I want to use a button or anchor tag then trigger the file to download. Is the GET and POST method can somewhat do it? or should I create a download.php file then call the download.php file when button is click?
So just create a link to the file containing the script above.
Standard disclaimer: Do not copy text from sites on the web without their express permission or waived permission via text. Follow all copyright and reproduction notices posted on the site you’re taking information from.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.