I'm trying to get all span elements with class="msgsource" from my website's html code and then send it to the browser to download as a .txt file. this is what I have, but it download an empty text file
<?php
// parse div content
include('simple_html_dom.php');
$html = file_get_html('http://www.example.com');
$code = $html->find('[@class="msgsource"]');
$content = $code->outertext;
// send content to browser
header("Content-Type: application/force-download");
header("Content-Length: " . sizeof($content));
header('Content-Disposition: attachment; filename=mylog.txt');
echo $content;
?>
If you are using the library that I am thinking check this http://simplehtmldom.sourceforge.net/manual.htm, to find elements with specific class you change find param like this
$html->find('.msgsource');
You have there examples how to find all spans from the content too.
<?php
// parse div content
include('simple_html_dom.php');
$html = file_get_html('http://simplehtmldom.sourceforge.net/manual.htm');
$code = $html->find('span[class=comment]');
echo count($code);
echo $code[1];
?>
Use this format to find html elements :
$code = $html->find('span[class=comment]');
The resultant will be stored in $code as an array and then access individual array elements (html element text contents) using their indexes.
Let's start with a simple case that shows that it should work:
$str = <<<EOF
<div class="msgsource">foo</div>
<div>bar</div>
<div class="msgsource">baz</div>
EOF;
$html = str_get_html($str);
$content = '';
foreach($html->find('.msgsource') as $code){
$content .= $code->outertext . "
";
}
echo $content;
From there, you can work backwards to try to figure out what's wrong in your case.