php导出不带元数据的文本文件


php export text file without meta data

我有一个字符串,其中包含来自SQL查询的数据,并尝试将其导出到文本文件。但是我只想要列namedata,我不想要元数据。我使用了以下代码:

header('Content-type: application/txt');
header('Content-Disposition: attachment; filename="day2.txt"');
echo "$header'n$data";

输出如下所示。如何仅获取body内部的内容?我不想要html标签。

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>"1340129" "2013-04-08" "true" "true"</body>
</html>

据我所知,您会问如何才能从html文档中只获取正文中的内容,从而将其作为文本文件。

您可以使用以下代码段进行

<?php
$d = new DOMDocument;
$m = new DOMDocument;
// In case if you have an html document in a variable or so
$d->loadHTML('<html><head></head><body>123</body></html>');
// You could use it in case html or so document is a file resource  
#$d->loadHtml(file_get_contents('/path/to/desired/file.html'));
$b = $d->getElementsByTagName('body')->item(0);
foreach ($b->childNodes as $child) {
    $m->appendChild($m->importNode($child, true));
}
// $m->saveHTML(); will hold only 123 in this case
echo $m->saveHTML();
?>

如果要删除所有HTML并只获取内容,请使用以下

而不是

echo"$header''n$data";

使用

echo strip_tags($header."''n".$data);