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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀