dp9599 2017-08-19 19:27
浏览 52
已采纳

如何解析PHP中的自定义.chart数组?

I'm parsing custom data that derives from .chart files.

Here's a snippet of what the syntax is like:

[Song]
{
  Player2 = bass
  Difficulty = 0
  PreviewStart = 0.00
  PreviewEnd = 0.00
  Genre = "rock"
}
[SyncTrack]
{
  0 = B 125000
  0 = TS 1
  768 = TS 4
}
[Events]
{
  0 = E "section Verse I"
  19200 = E "section Break I"
  25344 = E "section Verse II"
  31488 = E "section Verse III"
  37632 = E "section Orchestral Bridge 1"
  43776 = E "section Break II"
  49920 = E "section Chord Hero I"
}
[ExpertSingle]
{
  768 = N 2 192
  1056 = N 3 192
  1344 = N 4 168
  1536 = N 2 192
  1824 = N 1 192
  2112 = N 0 168
}

I've attempted to parse this data. Here's a snippet of my messy code so far:

$music_key_data = explode('[ExpertSingle]', $data);
$music_key_data = str_replace('}', '', $music_key_data);
$music_key_data = str_replace('{', '', $music_key_data);
$music_key_data = array_filter(preg_split ('/$\R?^/m', $music_key_data[1]));

foreach ($music_key_data as $var) {

    $music_data = explode(' ', $var);

    $data1 = $music_data[2];
    $data2 = $music_data[5];
    $data3 = $music_data[6];

}

How should I go about parsing this array into something easy to use in PHP? What I've done so far all seems like too much of a mess

Am I going about this the right way or is there a better method for parsing data like this?

Thanks

EDIT:

Another attempt:

$data = array_filter(preg_split ('/$\R?^/m', $data));


$array = array();
$inside_array = false;


foreach ($data as $var) {


    if ($inside_array === true && $var !== '{' && $var !== '}') {

        if (strpos($var, '=') !== false) {
            $var_explode = explode('=', $var);
            $array[$parent_name][ trim($var_explode[0]) ][] = trim($var_explode[1]);
        } else {
            $array[$parent_name][] = $var;
        }
    }


    if (strpos($var, '[') !== false && strpos($var, ']') !== false) {
        $parent_name = str_replace(array('[', ']'), array('', ''), $var);
        $inside_array = true;
    }

}


print_r($array);
die;
  • 写回答

3条回答 默认 最新

  • dongxu3029 2017-08-19 19:45
    关注

    This should return the data parsed as an associative array. Each value is treated as a string, so you can parse that the way you want lateron.

    function parseChart($str){
        $lines = explode(PHP_EOL,$str); // split lines into array
        $line_count = count($lines);
    
        $current_label = null;
        $current_object = null;
    
        $data = array(); // output data
    
        for($i=0;$i<$line_count;$i++){
    
            $line = trim($lines[$i]); // remove spaces & tabs from start and end of the current line
    
            if(strpos($line, '[')===0){ // if line starts with [
    
                $current_label = substr($line,1,-1); // set current label
    
            }else if(strpos($line, '{')===0){ // if line starts with {
    
                $current_object = array(); // create new object
    
            }else if(strpos($line, '}')===0){ // if line starts with }
    
                if(isset($current_label))
                    $data[$current_label] = $current_object; // write out new object into the outpdata with the label that has been set beforehand
    
            }else{
    
                $equals_index = strpos($line, '='); // get index of =
                if($equals_index!==false || !isset($current_object)){ // no = found or no label set -> ignore line
    
                    $key = trim(substr($line,0,$equals_index));
                    $value = trim(substr($line,$equals_index+1));
    
                    $current_object[$key] = $value;
    
                }
    
            }
        }
        return $data;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器