How Do I Remove Quotation From Csv?

- 1 answer

How do I remove quotation from csv?


use Goutte\Client;

$client = new Client();

$response = $client->request('GET', '');
$login_form = $response->filter('form')->form();
$login_form["account"] = '1';
$login_form["password"] = 'rpa1001';
$client->request('GET', '');
$csvResponse = $client->getResponse()->getContent();
return $csvResponse;

response is ...

213,契約中,個人,"鶴田 秀夫","ツルタ ヒデオ",2000/07/24,1508207,神奈川県吉田市北区佐々木町小林7-4-5,[email protected],0310-282-609,0730-327-581,,"2010-01-25 00:00:
00","2010-01-25 00:00:00"\r\n                                                                                                                                                     
221,契約中,個人,"桑原 彩羅","クワハラ サイラ",2008/04/03,8103797,青森県杉山市西区石田町浜田3-4-10,[email protected],090-5710-4350,03849-5-5746,,"2010-01-09 00:00:00","2010-0
1-09 00:00:00"\r\n                                                                                                                                                                
237,契約中,個人,"堤 悟志","ツツミ サトシ",2001/04/29,6875750,栃木県佐々木市東区中島町浜田6-6-6,[email protected],022-557-4260,0573-01-2822,,"2010-02-07 00:00:00","2010-02-07 00:0
273,契約中,個人,"富永 圭三","トミナガ ケイゾウ",2003/03/16,6314524,静岡県若松市東区廣川町青山10-9-6,[email protected],0020-062-493,06-3862-0779,,"2010-02-13 00:00:00","2
010-02-13 00:00:00"\r\n 

What I want to do

I want to remove quotation from csv.


213,契約中,個人,"鶴田 秀夫","ツルタ ヒデオ",2000/07/24,1508207,


213,契約中,個人,鶴田 秀夫,ツルタ ヒデオ,2000/07/24,1508207,

※This personal information is fake.

What I did

  1. str_replace(""", "", $csvResponse);
  2. str_replace("\xEF\xBB\xBF", '', $csvResponse);

but, not working



The quotes serve a purpose here so you shouldn't be removing them haphazardly. The quotes prevent columns from breaking when they need to contain text that can also contain the delimiter. Like "Foo, bar, baz" for example. Removing the quotes turns this one column into 3 columns, which is obviously wrong.

Though, if you did want to remove them str_replace would certainly not be the way to go, because the quotes can be escaped to be literals inside the column. For example, using str_replace on this column: "He said \"this is crazy\", and left." would strip literals from the column value.

Instead you should load the CSV data with fgetcsv() or str_getcsv() and rebuild the CSV without the quotes like so...

foreach (str_getcsv($csvData) as $row) {
    echo implode(",", $row), "\n";

This will give you back the literal values of each row without the quotes. Though any literal quotes inside those values will become quotes.