2017-08-06 121 views
0

我試圖將自定義格式的.txt文件轉換爲數組。解析數組的自定義格式

這裏是第一線的樣本:

[LINETYPE]S [STARTTIME]00:00:00 
[LINETYPE]M [TITLE]There For You [PERFORMER]Martin Garrix & Troye Sivan [MUSICID]21120 [LABEL]  
[LINETYPE]M [TITLE]Shut Up and Dance [PERFORMER]Walk The Moon [MUSICID]20634 [LABEL] 

繼續這樣跌下去,用一個新的[LINETYPE] S爲一天的每一個小時。

現在,我想達到的目標,是讓每一個小時的陣列 - 包含另一個數組與支架作爲鍵的每個值:

array("00:00:00" => array(

      array("LINETYPE" => "M", 
      "TITLE" => "There For You", 
      "PERFORMER" => "Martin Garrix & Troye Sivan", 
      "MUSICID" => "21120", 
      "LABEL" => ""), 

      array("LINETYPE" => "M", 
      "TITLE" => "Shut Up And Dance", 
      "PERFORMER" => "Walk The Moon", 
      "MUSICID" => "20634", 
      "LABEL" => "") 
    ), "01:00:00" => array(
    [...] 
); 

的正則表達式來搜索每個小時應該是正確的,但似乎preg_split不支持多個捕獲組。

到目前爲止,我發現的所有答案都在一個更基本的層面上,如爆炸逗號。

這是我的代碼到目前爲止。

$test = get_musiclog("06082017"); 

$hours = array(); 

$regex = "(\[LINETYPE\]S\t\[STARTTIME\])((?:(?:[0-2][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9])(?::[0-5][0-9])?(?:\\s?)?)\n"; 

if (preg_match_all ("/".$regex."/is", $test, $matches)) { 
    foreach($matches[2] as $hour) { 
     $hours[$hour] = ""; // Music array 
    } 
} 

幾乎沒有任何頭髮留在頭腦裏想着這些問題。任何人都可以將我指向正確的方向嗎?

  • 我如何拆分字符串 「[LINETYPE] S [STARTTIME] XX:XX:XX」 和保持時間的關鍵?
  • 如何知道結構是[鍵]值\ t而\ n是分隔符的其餘元素?
  • 「數組內的數組」是否正確?
+0

不要建立一個模式來匹配所有,使用* stream_get_line塊讀取文件*以字符串' [LINETYPE] S'(例如)作爲第三個參數。 –

+0

不確定''LINETYPE「=>」M「'是一個需要保留的相關信息,它似乎與文件格式本身更相關。 –

回答

0

我的方法主要使用否定字符類來捕獲標記之間的值。爲了保持正則表達式相對較輕,可以使用可選的捕獲組,以便像LINETYPE S這樣的短線仍匹配。

代碼:(Pattern Demo)(PHP Demo

$input='[LINETYPE]S [STARTTIME]00:00:00 
[LINETYPE]M [TITLE]There For You [PERFORMER]Martin Garrix & Troye Sivan [MUSICID]21120 [LABEL]  
[LINETYPE]M [TITLE]Shut Up and Dance [PERFORMER]Walk The Moon [MUSICID]20634 [LABEL] 
[LINETYPE]M [TITLE]Subeme La Radio [PERFORMER]Enrique Iglesias [MUSICID]21105 [LABEL]  
[LINETYPE]C [TITLE]AUTO S2 NON STOP [PERFORMER] [MUSICID]A2-6 [LABEL]  
[LINETYPE]M [TITLE]Uptown Funk [PERFORMER]Mark Ronson & Bruno Mars [MUSICID]20533 [LABEL]  
[LINETYPE]M [TITLE]Let It Go [PERFORMER]James Bay [MUSICID]20839 [LABEL]  
[LINETYPE]S [STARTTIME]01:00:00 
[LINETYPE]M [TITLE]There For You [PERFORMER]Martin Garrix & Troye Sivan [MUSICID]21120 [LABEL]  
[LINETYPE]M [TITLE]Shut Up and Dance [PERFORMER]Walk The Moon [MUSICID]20634 [LABEL]  '; 
$pattern='/\[LINETYPE\]([A-Z])\s\[(?:TITLE|STARTTIME)\]([^[\s]*(?: [^[\s]+)*)(?:\s*\[PERFORMER\]([^[ ]*(?: [^[ ]+)*)\s+\[MUSICID\]([^[ ]+)\s+\[LABEL\](\S*(?: \S+)*))?/'; 
if(!preg_match_all($pattern,$input,$out, PREG_SET_ORDER)){ 
    echo 'something went wrong'; 
}else{ 
    foreach($out as $a){ 
     if($a[1]=='S'){ 
      $time=$a[2]; 
     }else{ 
      $result[$time][]=["LINETYPE"=>$a[1],"TITLE"=>$a[2],"PERFORMER"=>$a[3],"MUSICID"=>$a[4],"LABEL"=>$a[5]]; 
     } 
    } 
} 
var_export($result); 

輸出:

array (
    '00:00:00' => 
    array (
    0 => 
    array (
     'LINETYPE' => 'M', 
     'TITLE' => 'There For You', 
     'PERFORMER' => 'Martin Garrix & Troye Sivan', 
     'MUSICID' => '21120', 
     'LABEL' => '', 
    ), 
    1 => 
    array (
     'LINETYPE' => 'M', 
     'TITLE' => 'Shut Up and Dance', 
     'PERFORMER' => 'Walk The Moon', 
     'MUSICID' => '20634', 
     'LABEL' => '', 
    ), 
    2 => 
    array (
     'LINETYPE' => 'M', 
     'TITLE' => 'Subeme La Radio', 
     'PERFORMER' => 'Enrique Iglesias', 
     'MUSICID' => '21105', 
     'LABEL' => '', 
    ), 
    3 => 
    array (
     'LINETYPE' => 'C', 
     'TITLE' => 'AUTO S2 NON STOP', 
     'PERFORMER' => '', 
     'MUSICID' => 'A2-6', 
     'LABEL' => '', 
    ), 
    4 => 
    array (
     'LINETYPE' => 'M', 
     'TITLE' => 'Uptown Funk', 
     'PERFORMER' => 'Mark Ronson & Bruno Mars', 
     'MUSICID' => '20533', 
     'LABEL' => '', 
    ), 
    5 => 
    array (
     'LINETYPE' => 'M', 
     'TITLE' => 'Let It Go', 
     'PERFORMER' => 'James Bay', 
     'MUSICID' => '20839', 
     'LABEL' => '', 
    ), 
), 
    '01:00:00' => 
    array (
    0 => 
    array (
     'LINETYPE' => 'M', 
     'TITLE' => 'There For You', 
     'PERFORMER' => 'Martin Garrix & Troye Sivan', 
     'MUSICID' => '21120', 
     'LABEL' => '', 
    ), 
    1 => 
    array (
     'LINETYPE' => 'M', 
     'TITLE' => 'Shut Up and Dance', 
     'PERFORMER' => 'Walk The Moon', 
     'MUSICID' => '20634', 
     'LABEL' => '', 
    ), 
), 
) 
+0

這可以幫助我很長的路,謝謝!我試着運行整個日誌,發現如果沒有PERFORMER設置,數組會出現一些奇怪的情況,並且會拋出一些「Undefined offset」錯誤。第6行[此處]的示例(http://sandbox.onlinephpfunctions.com/code/4006fcc4e70a51669e76074fc20201e1568cb7ec)。 – Jonas

+0

對不起,我的原始文章格式不正確。每個條目都是分開的。 – Jonas

+1

我爲它提供了所有可能的日誌輸出,它就像一個魅力!謝謝! – Jonas