[ create a new paste ] login | about

Link: http://codepad.org/InpHnU5f    [ raw code | output | fork ]

PHP, pasted on Nov 7:
<?php
    
      $data = <<<EOD
AU  - Author 1
AU  - Author 2
AU  - Author 3
LA  - ENG
PT  - ARTICLE
DEP - 235234
TA  - TA
JN  - Journal name
JID - 3456346
EDAT- 2011-11-03 06:00
MHDA- 2011-11-03 06:00
CRDT- 2011-11-03 06:00
TI  - multi-line text text text text text
      text text tex tex text
      text text tex tex text
EOD;

      // first, split into lines
      $lines = explode("\n",str_replace(array("\r\n","\r"),"\n",$data));
    
      // this will hold the parsed data
      $result = array();
    
      // This will track the current key for multi-line values
      $thisKey = '';
    
      // Loop the split data
      foreach ($lines as $line) {
        if (substr($line,4,1) == '-') {
          // There is a separator, start a new key
          $thisKey = trim(substr($line,0,4));
          if ($result[$thisKey]) {
            // This is a duplicate value
            if (is_array($result[$thisKey])) {
              // already an array
              $result[$thisKey][] = trim(substr($line,5));
            } else {
              // convert to array
              $result[$thisKey] = array($result[$thisKey],trim(substr($line,5)));
            }
          } else {
            // Not a duplicate value
            $result[$thisKey] = trim(substr($line,5));
          }
        } else {
          // There is no separator, append data to the last key
          $result[$thisKey] .= PHP_EOL.trim(substr($line,5));
        }
      }

      print_r($result);

?>


Output:
Array
(
    [AU] => Array
        (
            [0] => Author 1
            [1] => Author 2
            [2] => Author 3
        )

    [LA] => ENG
    [PT] => ARTICLE
    [DEP] => 235234
    [TA] => TA
    [JN] => Journal name
    [JID] => 3456346
    [EDAT] => 2011-11-03 06:00
    [MHDA] => 2011-11-03 06:00
    [CRDT] => 2011-11-03 06:00
    [TI] => multi-line text text text text text
text text tex tex text
text text tex tex text
)


Create a new paste based on this one


Comments: