1

I have this file, I cant figure out how to parse this file.

type = 10
version = 1.2
PART
{
    part = foobie
    partName = foobie
    EVENTS
    {
        MakeReference
        {
            active = True
        }
    }
    ACTIONS
    {
    }
}
PART
{
    part = bazer
    partName = bazer
}

I want this to be a array which should look like

$array = array(
    'type' => 10,
    'version' => 1.2,
    'PART' => array(
        'part' => 'foobie',
        'partName' => 'foobie,
        'EVENTS' => array(
            'MakeReference' => array(
                'active' => 'True'
            )
        ),
        'ACTIONS' => array(
        )
    ),
    'PART' => array(
        'part' => 'bazer',
        'partName' => 'bazer'
    )
);

I tried with preg_match but that was not a success. Any ideas?

Martin-
  • 876
  • 2
  • 13
  • 30
  • Where does the format come from? Why not just use a format that PHP can read, like JSON, INI, XML, etc.? – Sverri M. Olsen Jan 20 '13 at 16:49
  • You should show what you've tried. Without showing own effort this really becomes a "please write that parser for me" type of question. – mario Jan 20 '13 at 16:49
  • 2
    It just occurred to me that the format looks like TypoScript (used by TYPO3). Is that so? If so then you should look into TYPO3's parser. – Sverri M. Olsen Jan 20 '13 at 16:54
  • 1
    You cannot have 2+ identical keys in array (in your example `PART`). – Glavić Jan 20 '13 at 18:19
  • Sverri, nope its not typoscript, its actually a export file from a verrrry old POS system, which should be converted to something that can be imported into a webshop. But maybe actually the typoscript parser could be used, Ill look into that, thanks – Martin- Jan 22 '13 at 05:58

2 Answers2

1

Why not use a format that PHP can decode natively, like JSON?

http://php.net/json_decode

$json = file_get_contents("filename.txt");
$array = json_decode($json);
print_r($array);
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

Here is my approach.

First of all change Im changing the { to the line before. Thats pretty easy

$lines = explode("\r\n", $this->file);
foreach ($lines as $num => $line) {
    if (preg_match('/\{/', $line) === 1) {
       $lines[$num - 1] .= ' {';
       unset($lines[$num]);
    }
}

Now the input looks like this

PART {
    part = foobie

Now we can make the whole thing to XML instead, I know that I said a PHP array in the question, but a XML object is still fine enough.

foreach ($lines as $line) {
            if (preg_match('/(.*?)\{$/', $line, $matches)) {
                $xml->addElement(trim($matches[1]));
                continue;
            }

            if (preg_match('/\}/', $line)) {
                $xml->endElement();
                continue;
            }

            if (strpos($line, ' = ') !== false) {
                list($key, $value) = explode(' = ', $line);
                $xml->addElementAndContent(trim($key), trim($value));
            }
}

The addElement, actually just adds a to a string endElement adds a and addElementAndContent doing both and also add content between them.

And just for making the whole content being a XML object, im using

$xml = simplexml_load_string($xml->getXMLasText());

And now $xml is a XML object, which is so much easier to work with :)

Martin-
  • 876
  • 2
  • 13
  • 30