title: Parse text containing ini-formatted data to php array
body: <p>I have an API response in the following format</p>
  <pre><code>0000035901SM000000980000009800000125
  0J0005              00064182
                      00000000
  BEGIN
  DATE=26.05.2015 09:01:48
  SESSION=49500000031432619992
  ERROR=0
  RESULT=0
  TRANSID=1000005699140
  
  END
  BEGIN SIGNATURE
  iQBRAwkBAAD6tlVkDEwBAVOkAgCXHs1kj8u7E6tsdQLP8SFfkSDh9eYxkDYl/JdO
  +2Lekurt2TfL68wxbCdaaWoT9Jy71luAvyAkgtNte/1FT22dsAHH
  =GPcz
  END SIGNATURE
  </code></pre>
  <p>I need to parse the texts between BEGIN and END into an array like</p>
  <pre><code>array(
  'DATE'=&gt;'26.05.2015 09:01:48',...
  )
  </code></pre>
  <p>What is the best way to parse it. Also the content inside BEGIN/END can change.I wanted to create a generic function so that any key values inside BEGIN/END can be parsed out.</p>
  <p>I have tried this:</p>
  <pre><code>function parseData(array &amp;$lines)
  {
      $result = array();                                 // this is where the current block is stored
  
      // Process one line at a time...
      do {
          $l = trim(array_shift($lines));                // extract the first line into $l (and remove it from $lines)
          $p = str_getcsv($l, ' ', '&quot;');                 // split $l into words (take care of strings eclosed in quotes)
  
          $key = array_shift($p);                        // get the first word from the line
          switch ($key)
          {
          default:                                       // simple &quot;key value&quot; lines
              $result[$key] = implode(' ', $p);          // combine the remaining words from $p into a single string
              break;
  
          case 'BEGIN':                                  // start a new block
              $key = array_shift($p);                    // the real key in the array follows the 'BEGIN' keyword
              if (end($p) == 'END') {                    // check if the entire block is on a single line
                  array_pop($p);                         // remove 'END' from the end of the array
                  if (count($p) == 1) {                  // only one word remaining in $p:
                       $result[$key] = array_shift($p);  //              it is the value
                  } else {                               // more than one word in $p: this is a list of properties
                       $aa = array();                    // put the values of this one-line block here
                       while (count($p) &gt; 1) {
                           $k = array_shift($p);         // they come in pairs: key followed by value
                           $v = array_shift($p);
                           $aa[$k] = $v;
                       }
                       $result[$key] = $aa;              // the entire line was parsed; store it
                  }
              } else {                                   // the last word on the line is not 'END'
                  $result[$key] = parseData($lines);     // recursively call this function to parse the inner block and store its result
              }                                          // it will return after it consumes the line that starts with 'END'
              break;
  
          case 'END':                                    // here ends the block
              return $result;                            // return the collected values
          }
  
      } while (count($lines));                           // ... until there are no more lines to process
  
      return $result;                                    // no more lines to process; return the collected data
  }
  </code></pre>