在php中阅读此代码

时间:2014-04-13 11:27:38

标签: php arrays

我有一些像这样的代码,我想用PHP以数组格式阅读它。

    "Vehicle"
{
"WheelsPerAxle"     "2"
"Body"
{
    "CounterTorqueFactor"   "0.9"
    "MassCenterOverride"    "0 -3 13"
    "MassOverride"          "1467"
    "AddGravity"            "0.7"
    "MaxAngularVelocity"    "720"
}
"Engine"
{
    "HorsePower"            "301"
    "MaxRPM"                "4200"
    "MaxSpeed"              "95"
    "MaxReverseSpeed"       "28"
    "AutobrakeSpeedGain"    "1.1"
    "AutobrakeSpeedFactor"  "3"
    "Autotransmission"      "0"
    "AxleRatio"             "4.56"
    "Gear"                  "3.2"
    "Gear"                  "2.4"
    "Gear"                  "1.5"
    "Gear"                  "1"
    "Gear"                  "0.84"

    "ShiftUpRPM"            "3800"
    "ShiftDownRPM"          "1600"
}
"Steering"
{
    "DegreesSlow"                       "40"
    "DegreesFast"                       "12"
    "DegreesBoost"                      "11"
    "FastDampen"                        "0"
    "SteeringExponent"                  "1.4"
    "SlowCarSpeed"                      "12"
    "FastCarSpeed"                      "60"
    "SlowSteeringRate"                  "3"
    "FastSteeringRate"                  "2"
    "SteeringRestRateSlow"              "3"
    "SteeringRestRateFast"              "2"
    "TurnThrottleReduceSlow"            "0.01"
    "TurnThrottleReduceFast"            "0.6"
    "BrakeSteeringRateFactor"           "3"
    "ThrottleSteeringRestRateFactor"    "2"
    "BoostSteeringRestRateFactor"       "1.7"
    "BoostSteeringRateFactor"           "1.7"

    "PowerSlideAccel"                   "250"

    "SkidAllowed"                       "1"
    "DustCloud"                         "1"
}
"Axle"
{
    "Wheel"
    {
        "Radius"                        "15"
        "Mass"                          "270"
        "Inertia"                       "0"
        "Damping"                       "0.3"
        "RotDamping"                    "0.2"
        "Material"                      "jeeptire"
        "SkidMaterial"                  "slidingrubbertire"
        "BrakeMaterial"                 "brakingrubbertire"
    }
    "Suspension"
    {
        "SpringConstant"                "90"
        "SpringDamping"                 "0.5"
        "StabilizerConstant"            "0"
        "SpringDampingCompression"      "7"
        "MaxBodyForce"                  "10"
    }
    "TorqueFactor"                      "0.95"
    "BrakeFactor"                       "0.4"
}
"Axle"
{
    "Wheel"
    {
        "Radius"                        "15"
        "Mass"                          "230"
        "Inertia"                       "0"
        "Damping"                       "0"
        "RotDamping"                    "0"
        "Material"                      "jeeptire"
        "SkidMaterial"                  "slidingrubbertire"
        "BrakeMaterial"                 "brakingrubbertire"
    }
    "Suspension"
    {
        "SpringConstant"                "70"
        "SpringDamping"                 "0.5"
        "StabilizerConstant"            "0"
        "SpringDampingCompression"      "7"
        "MaxBodyForce"                  "15"
    }
    "TorqueFactor"                      "0"
    "BrakeFactor"                       "0.6"
}
}

  "Vehicle_Sounds"
   {
"Gear"
{
    "Max_Speed"             "0.06"
    "Speed_Approach_Factor" "1"
}
"Gear"
{
    "Max_Speed"             "0.25"
    "Speed_Approach_Factor" "0.08"
}
"Gear"
{
    "Max_Speed"             "0.35"
    "Speed_Approach_Factor" "0.05"
}
"Gear"
{
    "Max_Speed"             "0.55"
    "Speed_Approach_Factor" "0.035"
}
"Gear"
{
    "Max_Speed"             "1"
    "Speed_Approach_Factor" "0.01"
}
"State"
{
    "Name"      "SS_SHUTDOWN_WATER"
    "Sound"     "atv_stall_in_water"
    "Min_Time"  "0"
}
"State"
{
    "Name"      "SS_GEAR_2_RESUME"
    "Sound"     "vehicles\tdmcars\focusrs\third.wav"
    "Min_Time"  "0.5"
}
"State"
{
    "Name"      "SS_START_WATER"
    "Sound"     "atv_start_in_water"
    "Min_Time"  "0"
}
"State"
{
    "Name"      "SS_GEAR_3_RESUME"
    "Sound"     "vehicles\tdmcars\focusrs\fourth_cruise.wav"
    "Min_Time"  "0.5"
}
"State"
{
    "Name"      "SS_REVERSE"
    "Sound"     "vehicles\tdmcars\focusrs\rev.wav"
    "Min_Time"  "0.5"
}
"State"
{
    "Name"      "SS_IDLE"
    "Sound"     "vehicles\tdmcars\focusrs\idle.wav"
    "Min_Time"  "0"
}
"State"
{
    "Name"      "SS_GEAR_1_RESUME"
    "Sound"     "vehicles\tdmcars\focusrs\first.wav"
    "Min_Time"  "0.5"
}
"State"
{
    "Name"      "SS_GEAR_4"
    "Sound"     "vehicles\tdmcars\focusrs\fourth_cruise.wav"
    "Min_Time"  "0.5"
}
"State"
{
    "Name"      "SS_GEAR_1"
    "Sound"     "vehicles\tdmcars\focusrs\first.wav"
    "Min_Time"  "0.5"
}
"State"
{
    "Name"      "SS_SLOWDOWN_HIGHSPEED"
    "Sound"     "vehicles\tdmcars\focusrs\throttle_off.wav"
    "Min_Time"  "0"
}
"State"
{
    "Name"      "SS_SHUTDOWN"
    "Sound"     "vehicles\tdmcars\slsamg\engineoff.mp3"
    "Min_Time"  "0"
}
"State"
{
    "Name"      "SS_START_IDLE"
    "Sound"     "vehicles\tdmcars\slsamg\enginestart.mp3"
    "Min_Time"  "0.3"
}
"State"
{
    "Name"      "SS_GEAR_4_RESUME"
    "Sound"     "vehicles\tdmcars\focusrs\fourth_cruise.wav"
    "Min_Time"  "0.5"
}
"State"
{
    "Name"      "SS_GEAR_3"
    "Sound"     "vehicles\tdmcars\focusrs\third.wav"
    "Min_Time"  "0.5"
}
"State"
{
    "Name"      "SS_GEAR_0_RESUME"
    "Sound"     "vehicles\tdmcars\focusrs\first.wav"
    "Min_Time"  "0.75"
}
"State"
{
    "Name"      "SS_GEAR_0"
    "Sound"     "vehicles\tdmcars\focusrs\idle.wav"
    "Min_Time"  "0.08"
}
"State"
{
    "Name"      "SS_GEAR_2"
    "Sound"     "vehicles\tdmcars\focusrs\second.wav"
    "Min_Time"  "0.5"
}
"State"
{
    "Name"      "SS_SLOWDOWN"
    "Sound"     "vehicles\tdmcars\focusrs\idle.wav"
    "Min_Time"  "0"
}
"CrashSound"
{
    "Min_Speed"         "350"
    "Min_Speed_Change"  "250"
    "Sound"             "atv_impact_medium"
    "Gear_Limit"        "1"
}
"CrashSound"
{
    "Min_Speed"         "450"
    "Min_Speed_Change"  "350"
    "Sound"             "atv_impact_heavy"
    "Gear_Limit"        "0"
}

"Skid_LowFriction"      ""
"Skid_NormalFriction"   ""
"Skid_HighFriction"     ""
}

我不知道应该如何开始

键总是一样的

有人可以给我一个关于如何解析这样一个文件的提示吗?

提前致谢

2 个答案:

答案 0 :(得分:0)

看起来你应该为这种格式编写自己的解析器。你可以尝试找一个解析器生成器来从一些中间语法生成一个解析器,或者你可以从头开始编写它。我认为从头开始编写它是可行的。

我已经给了它一个镜头并为你实现了一个演示解析器,解析器只是你需要的一部分。评论应该解释得足以让你开始。

您需要特别注意的是,当您开始处理花括号时,您需要递归调用。祝你好运!

<?php
// simple demo parser that will parse tokens surrounded by " from a file that
// only contains whitespace in the form of spaces or newlines aside from these
// tokens
// For example something like this:
/*
"hello"

    "this"

  "is"

    "a"



 "Test"
   "10.9"
 */


// parse a token from the string
// assume opening " has already been encountered
// note the pass by reference!
function parse_token($textData, &$index, &$token) {
    $token = "";
    $length = strlen($textData);
    while ($index < $length) {

        // get next character
        $char = $textData{$index};

        // increment for next round
        // do this here because we want to increment before breaking
        // out of the loop
        $index++;

        // anything that is not " is part of the token
        if ("\"" != $char) {
            $token .= $char;
        } else {
            break;
        }

    }
}

// main function to do the parsing
// note the pass by reference!
function do_parse($textData, &$index, &$output) {
    $output = [];
    $char = NULL;
    $length = strlen($textData);
    $token = NULL;
    while ($index < $length) {

        // get next character
        $char = $textData{$index};

        // if we encounter an opening "
        if ("\"" == $char) {
            // parse the rest as a token, and store it in our output array
            $output[] = parse_token($textData, ++$index, $token);
        } else if (" " == $char || "\n" == $char) {
            // skip whitespace, go on to next char
            $index++;
        } else {
            throw new Exception('unexpected character ' . $char);
        }
    }
}

// read file into string
$data = file_get_contents ("data.txt");

// perform actual parsing
$index = 0;
do_parse($data, $index, $parsed);

// print result
print_r($parsed);

答案 1 :(得分:-1)

我认为这是一个LUA数组,你尝试阅读,所以你需要一个lua&gt; php转换器。这是一个网站,提供有关lua到php的一些信息。

info lua to php

希望这会对你有所帮助。