Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6779661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:24:44+00:00 2026-05-26T16:24:44+00:00

I have to convert a long string of data into values so that I

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:24:45+00:00Added an answer on May 26, 2026 at 4:24 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to convert an incoming String field into a BigDecimal field that would
I have a function that convert ticks to time_t format long ticks = DateTime.Now.Ticks;
Does anyone have any suggestions on how to convert data (mostly strings) into their
I have some rather long string containing just about anything that I want to
I am doing RSA encryption and I have to split my long string into
I often have to convert a retreived value (usually as a string) - and
I have three projects written with VB.NET (2005) and have to convert them to
I have some unicode codepoints (\u5315\u4e03\u58ec\u4e8c\u4e0a\u53b6\u4e4b), which I have to convert into actual characters
I have a list of Phone Numbers which I need to convert into a
I have a string value that could contain a double, integer, ascii or byte

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.