【发布时间】:2016-11-21 15:29:55
【问题描述】:
我有一个 HTML 表单,我需要将其存储在 DB 中,然后将其写入 .csv 和 .txt 。一切正常,除了 txt 输出给了我一些错误的字符串。
fputcsv 函数:
private function fputcsv_custom1($fp, $array, $eol)
{
fputcsv($fp, $array, ';');
if ("\r\n" != $eol && fseek($fp, -1, SEEK_CUR) === 0)
{ fwrite($fp, $eol . "\r\n"); }
}
控制器:
$peoples = OnlineSz::all()->toArray();
$file = fopen("online_hotel.csv", "w+");
$file1 = fopen("online_hotel.txt", "w+");
array_keys($peoples);
foreach ($peoples as $key => $array) {
$this->fputcsv_custom($file, $array, ';');
$this->fputcsv_custom1($file1, $array, ';');
}
fclose($file);
fclose($file1);
它生成我 2 个文件,由 ; 分隔最后一个值给新行一个 \r\n。
txt输出是下一个:
53;"New test";Worker;test;test;4000;test;test;06123456789;test;0;0;CC;;0;0;
如果字符串包含空格,它是由 " " 存储的,我怎样才能删除这个引号?或者我怎样才能只替换数组中这一列的引号?
感谢您的帮助!
编辑
如果我替换 2 个单词之间的空格,引号不存在,但是如果我需要字符串之间的空格,我该怎么办?
控制器:
$this->fputcsv_custom($file4, str_ireplace(chr(32), '/',$user), ';');
$this->fputcsv_custom1($file5, str_ireplace(chr(32), '/',$user), ';');
输出:
53;New/test;Alkalmazott;test;test;4000;test;test;06123456789;0;0;CC;0;0;
但我错了,因为 /,如果我将其替换为 chr(32),引号将在那里。
有什么办法吗?
【问题讨论】: