使用 php 从 MySQL 表创建 CSV 文件
此代码创建一个 csv 文件。但是,我避免在字段中打印出逗号,因为它用作分隔符(参见第 22 行)。现在我想从字段中删除(回车符和新行)。添加 $somecontent .= str_replace("\n", "", $val); 23号线好像不行。有什么想法吗?
@chmod($export_csv, 0777);
$fe = @fopen($export_csv."/export.csv", "w+");
if($fe){
$somecontent = "";
$fields_count = 0;
// print field headers
$db->query($sql_view);
if($row = $db->fetchAssoc()){
foreach($row as $key => $val){
if($fields_count++ > 0) $somecontent .= ",";
$somecontent .= ucfirst($key);
}
}
$somecontent .= "\n";
// print field values
$db->query($sql_view);
while($row = $db->fetchAssoc()){
$fields_count = 0;
foreach($row as $key => $val){
if($fields_count++ > 0) $somecontent .= ",";
$somecontent .= str_replace(",", "", $val);
$somecontent .= str_replace("\n", "", $val);
}
$somecontent .= "\n";
}
// write some content to the opened file.
if (fwrite($fe, $somecontent) == FALSE) echo 'file_writing_error'." (export.csv)";
fclose($fe);
}
this code creates a csv file. However, I have avoided printing out commas in the fields because it is used as a delimeter (see line 22). Now I want to remove (carriage returns and new lines) from the fields. Adding $somecontent .= str_replace("\n", "", $val); on line 23 does not seem to work. any ideas?
@chmod($export_csv, 0777);
$fe = @fopen($export_csv."/export.csv", "w+");
if($fe){
$somecontent = "";
$fields_count = 0;
// print field headers
$db->query($sql_view);
if($row = $db->fetchAssoc()){
foreach($row as $key => $val){
if($fields_count++ > 0) $somecontent .= ",";
$somecontent .= ucfirst($key);
}
}
$somecontent .= "\n";
// print field values
$db->query($sql_view);
while($row = $db->fetchAssoc()){
$fields_count = 0;
foreach($row as $key => $val){
if($fields_count++ > 0) $somecontent .= ",";
$somecontent .= str_replace(",", "", $val);
$somecontent .= str_replace("\n", "", $val);
}
$somecontent .= "\n";
}
// write some content to the opened file.
if (fwrite($fe, $somecontent) == FALSE) echo 'file_writing_error'." (export.csv)";
fclose($fe);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 fputcsv 函数写入 CSV 文件。 PHP 已经具有写入 csv 文件的功能。
您不需要自己处理转义、分隔符,所有这些都已处理。
这将为您处理所有这些事情,并让您免于许多麻烦。
Use fputcsv function to write the CSV files. PHP already have a function to write csv files.
You do not need to handle escaping, delimiter by yourself all these already handled.
This will handle all these things for you and will save you from many headaches.
您可以直接从 MySql 执行此操作:
You can do that from MySql directly:
你的
$somecontent .= str_replace("\n", "", $val)
在哪里?我在你的代码中找不到它。您可以指定一个数组作为
str_replace
的第一个参数,如例如,$somecontent .= str_replace(array("\n", "\r"), "", $val);
Where is your
$somecontent .= str_replace("\n", "", $val)
? I can't find it in your code.You can specify an array as first parameter for
str_replace
as indicated there. Thus, it would replace each string present in your array with the second parameter.For example,
$somecontent .= str_replace(array("\n", "\r"), "", $val);
你可以尝试这样的事情,玩 ASCII 字符
you can try something like this, playing with ASCII caracters