skiindude22
skiindude22

Reputation: 509

Converting a string of data to an array with PHP

I have to convert a long string of data into values so that I can import them into my database. Unfortunately, the data is displayed as text and not XML, so I need a way to convert this into, ideally, a key->value array.

The data looks like this:

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

After researching, it seems like explode could be a viable means to accomplish this, but I'm not sure how to implement it in this scenerio, or if there is a better method of accomplishing this. Especially since there can be random hyphens and line breaks in the middle of the string.

Any help much appreciated in advance!

Upvotes: 1

Views: 188

Answers (1)

DaveRandom
DaveRandom

Reputation: 88697

Since values can contain dashes and be spread across multiple lines, I think the safest method for separating keys from values is using substr(), since the separating dashes always sit at the same character position in the string.

FIXED

<?php

  // 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
      if (is_array($result[$thisKey])) {
        $result[$thisKey][count($result[$thisKey]) - 1] .= PHP_EOL.trim(substr($line,5));
      } else {
        $result[$thisKey] .= PHP_EOL.trim(substr($line,5));
      }
    }
  }

  print_r($result);

?>

See it working

Upvotes: 3

Related Questions