title: distinct empty strings from nulls in a CSV input
body: <p>I have database outputs like the following: </p>
  
  <p><code>$row = '(one,"pika chu",,"")'</code></p>
  
  <p>If I send this string as parameter to <a href="http://php.net/manual/en/function.str-getcsv.php" rel="nofollow noreferrer"><code>str_getcsv</code></a> it will output <code>['one', 'pika chu', '', '']</code>. The third element despite being absent has been turned into an empty string. This is very annoying since I must recognize empty values (no values) from empty strings. The output I would expect is <code>['one', 'pika chu', null, '']</code>.</p>
  
  <p>The inputs I get are from a PostgreSQL database and are represented as composite values. </p>
  
  <p>By example, if a table is <code>pokemon_id =&gt; int4, name =&gt; text</code> then a query will output strings like <code>'(1, "pika chu")'</code>. A unique constraint on the name field by example will allow the following two records to exist: <code>(100, '')</code> and <code>(101, null)</code>.</p>
  
  <p>When fetched, they are formatted as raw values like:
  <code>
  '98,whatever'
  '99,"pika chu"'
  '100,""'
  '101,'
  '102,","'
  </code></p>
  
  <p>I need to read those strings and this example must output the following arrays:
  <code>
  ['98', 'whatever']
  ['99', 'pika chu']
  ['100', '']
  ['101', null]
  ['102', ',']</code></p>
  
  <p>Is there a way to do that in PHP?</p>
  
  <p><strong>Update 1:</strong> @deceze kindly sent me <a href="http://www.garretwilson.com/blog/2009/04/23/csvnull.xhtml" rel="nofollow noreferrer">this link</a> stating there are no NULLs in CSV (TL;DR because there were no nulls in XML basically, this problem <a href="https://stackoverflow.com/questions/774192/what-is-the-correct-way-to-represent-null-xml-elements">has been tackled</a> since then.) How to parse CSV with NULLs then?</p>
  
  <p><strong>Update 2:</strong> I had propositions to create a dedicated parser in PHP using <code>preg_match_*</code> functions. I am a bit reluctant to go that way because 1) of the performance impact compared to <code>str_getcsv</code> and 2) the fact <code>preg_match</code> used to segfault if the string passed was over 8kb (which can happen in a CSV context).</p>
  
  <p><strong>Update 3:</strong> I looked at <code>str_getcsv</code><a href="http://lxr.php.net/xref/PHP-7.1/ext/standard/file.c#2037" rel="nofollow noreferrer">source code</a> to see if it was possible to propose a patch to add parsing options like it is in some other languages. I now understand PHP’s underlying philosophy better. @daniel-vérité raised the idea to implement a state machine to parse CSV strings. Even though input can have thousands of lines that weight dozens of kilobytes with embedded CSV structures, it might be the best way.</p>
  
  <p>Thank you for your help.</p>