Parsing Valve Data Format files in PHP

2019-03-01 23:45发布

I am completely stuck in how to start parsing these kind of files in PHP. These are some sort of arrays with variable amount of sub-arrays. https://developer.valvesoftware.com/wiki/KeyValues

Example file.

"itembuilds"

{

    "author"        "Valve"
    "hero"          "npc_dota_hero_axe"
    "Title"         "Recommended items for Axe"
    "Items"
    {
        "#DOTA_Item_Build_Starting_Items"
        {
            "item"   "item_stout_shield"
            "item"   "item_flask"
            "item"   "item_tango"
            "item"   "item_courier"
        }
        "#DOTA_Item_Build_Early_Game"
        {
            "item"   "item_boots"
            "item"   "item_ring_of_health"
            "item"   "item_tango"
            "item"   "item_tango"
        }
        "#DOTA_Item_Build_Core_Items"
        {
            "item"   "item_blink"
            "item"   "item_vanguard"
        }
        "#DOTA_Item_Build_Luxury"
        {
            "item"   "item_heart"
            "item"   "item_blade_mail"
        }
    }
}

Another example file.

"dota_shops"

{

    "consumables"
    {
        "item"      "item_clarity"
        "item"      "item_tango"
        "item"      "item_flask"
        "item"      "item_smoke_of_deceit"
        "item"      "item_tpscroll"
        "item"      "item_dust"
        "item"      "item_courier"
        "item"      "item_flying_courier"
        "item"      "item_ward_observer"
        "item"      "item_ward_sentry"
        "item"  "item_bottle"                           
    }
        "something"
        {
                "Key" "value"
                "Key2" "value"
        }
}

标签: php parsing
2条回答
啃猪蹄的小仙女
2楼-- · 2019-03-02 00:09

There an online one I found. http://rossengeorgiev.github.io/vdf-parser/

The repo has a js and python implementations, it shouldn't be too hard to port them to PHP.

查看更多
Melony?
3楼-- · 2019-03-02 00:12

Late answer, but just saw this. The data on question is in (Valve Data Format)[https://developer.valvesoftware.com/wiki/KeyValues], also called KeyValues format, and is very reminiscence of JSON.

About a year ago I wrote this script which can convert VDF data to valid JSON. You can use it once and store the generated JSON or call it anytime you need JSON from VDF.

https://gist.github.com/AlienHoboken/5571903

查看更多
登录 后发表回答