Parse JSON-like markup

php, regex

Solution

Here is my solution:

You have to feed into it only the insides of the {} tags, and you get an array that respects the string quotes etc.

Unlike the other answer, it takes care of `"text with quotes"` and accepts both spaces, tabs and mixed for formatting.

$a = <<<EOT
    commonName      "Birthday Cake"
    autoTag
    category        Item
    path            models/
    timedExclusive  1
    descSymbol      BirthdayCakeDesc
    dispSymbol      BirthdayCakeDisp
    flairCfg        "Cake/Idle.aaf mat_BirthdayCake< /scale 1.5 /animation Cake/Idle.aaf 0 looping 0.8  /followDist 3.0 /moveSlew 0.0666  /moveVelThresh 10.0 /animEaseTime 1.0 /zOffRobot 2.7 /rotX 20.0 /zSpinDef bone_spinA 80.0"
    //OnInspectOrUnlock Menus previewInit Cake/Idle.aaf BirthdayCake< /scale 1.5 /animation Cake/Idle.aaf 0 looping 0.8 /rotSpeed 30 /pos 3.5 0 1 /scaleMult 1.0
    property        BirthdaySpirit          10
EOT;

$lines = explode("\n", $a);

$parsed = array();

foreach($lines as $line) {
    $chars = str_split($line);

    $quoteOpen = false;

    $lastField = "";

    $lineFields = array();

    foreach($chars as $c) {
        if($c == '"') {
            if($quoteOpen) {
                $quoteOpen = false;
                $lineFields[] = $lastField;
            } else {
                $quoteOpen = true;
            }
            $lastField = "";
            continue;
        }

        if(preg_match("/\\s/", $c) === 0) {
            $lastField .= $c;
        } else {
            if($lastField != "" && !$quoteOpen) {
                $lineFields[] = $lastField;
                $lastField = "";
            } else {
                if(!$lastField == "") {
                    $lastField .= $c;
                }
            }
        }
    }

    if($lastField != "") {
        $lineFields[] = $lastField;
    }

    $parsed[] = $lineFields;
}

echo '<pre>'; print_r($parsed); echo '</pre>';

Output:

Array
(
    [0] => Array
        (
            [0] => commonName
            [1] => Birthday Cake
        )

    [1] => Array
        (
            [0] => autoTag
        )

    [2] => Array
        (
            [0] => category
            [1] => Item
        )

    [3] => Array
        (
            [0] => path
            [1] => models/
        )

    [4] => Array
        (
            [0] => timedExclusive
            [1] => 1
        )

    [5] => Array
        (
            [0] => descSymbol
            [1] => BirthdayCakeDesc
        )

    [6] => Array
        (
            [0] => dispSymbol
            [1] => BirthdayCakeDisp
        )

    [7] => Array
        (
            [0] => flairCfg
            [1] => Cake/Idle.aaf mat_BirthdayCake< /scale 1.5 /animation Cake/Idle.aaf 0 looping 0.8  /followDist 3.0 /moveSlew 0.0666  /moveVelThresh 10.0 /animEaseTime 1.0 /zOffRobot 2.7 /rotX 20.0 /zSpinDef bone_spinA 80.0
        )

    [8] => Array
        (
            [0] => //OnInspectOrUnlock
            [1] => Menus
            [2] => previewInit
            [3] => Cake/Idle.aaf
            [4] => BirthdayCake<
            [5] => /scale
            [6] => 1.5
            [7] => /animation
            [8] => Cake/Idle.aaf
            [9] => 0
            [10] => looping
            [11] => 0.8
            [12] => /rotSpeed
            [13] => 30
            [14] => /pos
            [15] => 3.5
            [16] => 0
            [17] => 1
            [18] => /scaleMult
            [19] => 1.0
        )

    [9] => Array
        (
            [0] => property
            [1] => BirthdaySpirit
            [2] => 10
        )

)

Problem

honestly i don't know what markup is this (i would like to know if this markup have a name). What is easyest way to parse structures like this? i have a lot of them in txt file. ``` unlockType BirthdayCake { // Don't delete commonName "Birthday Cake" autoTag category Item path models/ timedExclusive 1 descSymbol BirthdayCakeDesc dispSymbol BirthdayCakeDisp flairCfg "Cake/Idle.aaf mat_BirthdayCake< /scale 1.5 /animation Cake/Idle.aaf 0 looping 0.8 /followDist 3.0 /moveSlew 0.0666 /moveVelThresh 10.0 /animEaseTime 1.0 /zOffRobot 2.7 /rotX 20.0 /zSpinDef bone_spinA 80.0" //OnInspectOrUnlock Menus previewInit Cake/Idle.aaf BirthdayCake< /scale 1.5 /animation Cake/Idle.aaf 0 looping 0.8 /rotSpeed 30 /pos 3.5 0 1 /scaleMult 1.0 property BirthdaySpirit 10 } ```

Original source