Php 删除“;从数组中的字符串
我有一个HTML表单,我需要将其存储在DB中,然后将其写入.csv和.txt。一切都很好,除了txt输出给我一些坏字符串 fputcsv功能:Php 删除“;从数组中的字符串,php,mysql,laravel,fputcsv,Php,Mysql,Laravel,Fputcsv,我有一个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")
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);
$this->fputcsv_custom($file4, str_ireplace(chr(32), '/',$user), ';');
$this->fputcsv_custom1($file5, str_ireplace(chr(32), '/',$user), ';');
它生成me 2文件,文件之间用分隔符分隔;最后一个值为新行\r\n指定一个值
txt输出是下一个:
53;"New test";Worker;test;test;4000;test;test;06123456789;test;0;0;CC;;0;0;
如果字符串包含空格,则由“”存储,如何删除此引号?或者如何仅替换数组中此列的引号
谢谢你的帮助
编辑
如果我替换两个单词之间的空格,引号就不存在了,但是如果我需要字符串之间的空格,我该怎么做呢
控制器:
$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);
$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),引号就会出现
任何解决方案?使用str\u ireplace删除引号
$var = str_ireplace('"', '', $var); //strip quotes from $var
$this->fputcsv_custom($file, str_ireplace('"', '',$array), ';');
$this->fputcsv_custom1($file1, str_ireplace('"', '',$array), ';');
您可能还希望修剪它们的尾随空格
$this->fputcsv_custom($file, trim(str_ireplace('"', '',$array)), ';');
$this->fputcsv_custom1($file1, trim(str_ireplace('"', '',$array)), ';');
使用str_ireplace删除引号
$var = str_ireplace('"', '', $var); //strip quotes from $var
$this->fputcsv_custom($file, str_ireplace('"', '',$array), ';');
$this->fputcsv_custom1($file1, str_ireplace('"', '',$array), ';');
您可能还希望修剪它们的尾随空格
$this->fputcsv_custom($file, trim(str_ireplace('"', '',$array)), ';');
$this->fputcsv_custom1($file1, trim(str_ireplace('"', '',$array)), ';');
您可以使用内爆(;”,$array),而不是fputcsv(…)
代码>和fputs(…)
但请记住,csv php函数遵循某种RC标准,我现在无法在谷歌上搜索。稍后我将编辑答案。因此,您不再遵循标准。而不是fputcsv(…)
您可以使用内爆(;”,$array)代码>和fputs(…)
但请记住,csv php函数遵循某种RC标准,我现在无法在谷歌上搜索。稍后我将编辑答案。因此,您不再遵循标准。我已经使用了您的第一个答案,但在.txt中,引号仍然存在“
Odd..可能将看到的字符复制并粘贴到stru-ireplace('X'可能不是标准的引号,但仍在输出TXT文件中我使用了您的第一个答案,但在.TXT中,引号仍在那里..
Odd..可能将看到的字符复制并粘贴到stru-ireplace中。”('X'它可能不是标准的quotecopied,但仍在输出TXT文件中