note 108446 added to function.str-getcsv

From: Date: Fri, 27 Apr 2012 02:40:30 +0000
Subject: note 108446 added to function.str-getcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-187368@lists.php.net to get a copy of this message
Re: ioplex's comment (http://php.net/manual/en/function.str-getcsv.php#105981) that this function does not parse the csv format produced by Excel, in fact it does appear to (meaning the documentation is misleading). <?php $s='a,"b",c,"def""in quotes""xyz","'."one\ntwo\nthree".'","back\\slash"'; print_r(str_getcsv($s)); /* Array ( [0] => a [1] => b [2] => c [3] => def"in quotes"xyz [4] => one two three [5] => back\slash ) */ ?> I.e. it appears like $escape is not used, and that "" is correctly treated as an escaped quote. I've tried using different values for $escape and always get the above results. However, where str_fgetcsv() does go wrong is with embedded line-breaks when you give delimiter as "\n". Taking the example from the Wikipedia page: <?php $s2=<<<EOD Year,Make,Model,Description,Price 1997,Ford,E350,"ac, abs, moon",3000.00 1999,Chevy,"Venture ""Extended Edition""","",4900.00 1999,Chevy,"Venture ""Extended Edition, Very Large""","",5000.00 1996,Jeep,Grand Cherokee,"MUST SELL! air, moon roof, loaded",4799.00 EOD; $lines=str_getcsv($s2,"\n"); print_r($lines); /* Array ( [0] => Year,Make,Model,Description,Price [1] => 1997,Ford,E350,"ac, abs, moon",3000.00 [2] => 1999,Chevy,"Venture ""Extended Edition""","",4900.00 [3] => 1999,Chevy,"Venture ""Extended Edition, Very Large""","",5000.00 [4] => 1996,Jeep,Grand Cherokee,"MUST SELL! [5] => air, moon roof, loaded",4799.00 ) */ ?> NB. This disagrees with durik's comment (http://php.net/manual/en/function.str-getcsv.php#101888), which otherwise sounded like a great idea! P.S. examples are for php 5.3.2, Ubuntu. If you copy-and-pasted those code snippets and get different results, make sure the line breaks in your source code are LF (\n), not CRLF or CR. ---- Server IP: 49.212.134.217 Probable Submitter: 61.193.69.76 ---- Manual Page -- http://www.php.net/manual/en/function.str-getcsv.php Edit -- https://master.php.net/note/edit/108446 Del: integrated -- https://master.php.net/note/delete/108446/integrated Del: useless -- https://master.php.net/note/delete/108446/useless Del: bad code -- https://master.php.net/note/delete/108446/bad+code Del: spam -- https://master.php.net/note/delete/108446/spam Del: non-english -- https://master.php.net/note/delete/108446/non-english Del: in docs -- https://master.php.net/note/delete/108446/in+docs Del: other reasons-- https://master.php.net/note/delete/108446 Reject -- https://master.php.net/note/reject/108446 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#187368) next »