在php中写入csv(类php)
我正在使用php类将数据写入CSV。课程链接: 类别代码:在php中写入csv(类php),php,arrays,class,csv,Php,Arrays,Class,Csv,我正在使用php类将数据写入CSV。课程链接: 类别代码: <?php /** * Simple class to properly output CSV data to clients. PHP 5 has a built * in method to do the same for writing to files (fputcsv()), but many times * going right to the client is beneficial. * * @
<?php
/**
* Simple class to properly output CSV data to clients. PHP 5 has a built
* in method to do the same for writing to files (fputcsv()), but many times
* going right to the client is beneficial.
*
* @author Jon Gales
*/
class CSV_Writer {
public $data = array();
public $deliminator;
/**
* Loads data and optionally a deliminator. Data is assumed to be an array
* of associative arrays.
*
* @param array $data
* @param string $deliminator
*/
function __construct($data, $deliminator = ",")
{
if (!is_array($data))
{
throw new Exception('CSV_Writer only accepts data as arrays');
}
$this->data = $data;
$this->deliminator = $deliminator;
}
private function wrap_with_quotes($data)
{
$data = preg_replace('/"(.+)"/', '""$1""', $data);
return sprintf('"%s"', $data);
}
/**
* Echos the escaped CSV file with chosen delimeter
*
* @return void
*/
public function output()
{
foreach ($this->data as $row)
{
$quoted_data = array_map(array('CSV_Writer', 'wrap_with_quotes'), $row);
echo sprintf("%s\n", implode($this->deliminator, $quoted_data));
}
}
/**
* Sets proper Content-Type header and attachment for the CSV outpu
*
* @param string $name
* @return void
*/
public function headers($name)
{
header('Content-Type: application/csv');
header("Content-disposition: attachment; filename={$name}.csv");
}
}
?>
问题是生成的文件。我有一个CSV文件而不是数组,只是HTML内容页。为什么会这样
CSV文件的内容:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>title</title>
<link href="../css/style.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="jscripts/picnet.table.filter.min.js"></script>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript">
...
标题
...
Thx请求帮助。您有两个问题:
exit()代码>该块末尾的行:
我不想打断你,但php内置了对编写csv文件的支持。。。这(几乎)完全不需要这个类。如果仍要将功能包装在类中以输出标题,请至少包装
fputcsv
,并且不要复制其功能。请参阅。或者至少悬停链接。你需要给我们更多的代码。您提供给我们的处理CSV创建的代码片段是完全正常和正常的。很有可能是其他东西在生成HTML并在CSV发出之前发出它。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>title</title>
<link href="../css/style.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="jscripts/picnet.table.filter.min.js"></script>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript">
...
if($_GET['action']=="csv") {
$data = array(array("one","two","three"), array(4,5,6));
$csv = new CSV_Writer($data);
$csv->headers('test');
$csv->output();
exit();
}