1

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!

0

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.