2010-05-24 77 views
0

我有一個字符串,像這樣:串解析幫助

#################### 
Section One 
#################### 
Data A 
Data B 


#################### 
    Section Two 
#################### 
Data C 
Data D 

etc. 

我想將它解析爲類似:

$arr(
    'Section One' => array('Data A', 'Data B'), 
    'Section Two' => array('Data C', 'Data D') 
) 

起初,我嘗試這樣做:

$sections = preg_split("/(\r?\n)(\r?\n)#/", $file_content); 

問題是,文件並非完全乾淨:有時段之間有空行數量不同,或數據行之間有空格。

科長模式本身似乎是比較一致:

#################### 
    Section Title 
#################### 

排名的數量,可能是一致的,但我不想指望它。標題行上的空格非常隨意。

一旦我將它分成幾個部分,我認爲它會非常簡單,但是如果你想幫助寫一個殺手reg來獲得它,那麼我們將會很感激。 (或者,如果有比REG前一個更好的辦法...)

回答

1

我能夠迅速地寫下這件事:

<?php 
$text = <<<EOT 
#################### 
Section One 
#################### 
Data B.Thing=bar# 
.##.#%# 

#################### 
    Empty Section! 
#################### 
#################### 
    Last section 
#################### 

Blah 

    Blah C# C# C# 

EOT; 
$entries = array_chunk(
    preg_split("/^#+/m", $text, null, PREG_SPLIT_NO_EMPTY), 
    2 
); 
$sections = array(); 
foreach ($entries as $entry) { 
    $key = trim($entry[0]); 
    $value = preg_split("/\n/", $entry[1], null, PREG_SPLIT_NO_EMPTY); 
    $sections[$key] = $value; 
} 
print_r($sections); 
?> 

輸出是:(as run on ideone.com

Array 
(
    [Section One] => Array 
     (
      [0] => Data B.Thing=bar# 
      [1] => .##.#%# 
     ) 

    [Empty Section!] => Array 
     (
     ) 

    [Last section] => Array 
     (
      [0] => Blah 
      [1] => Blah C# C# C# 
     ) 

) 
+0

太棒了,謝謝!但這並不奏效。 :(它似乎嗆在數據行中的非字母字符,我的所有數據行都有,因爲它們是名稱值對,如「foo.bar = baz」http://ideone.com/u3xYo – sprugman 2010-05-25 14:08:07

+0

@ sprugman,好吧,我不確定數據模式是什麼,但是如果你可以保證它永遠不會包含'#',(例如沒有''C#太棒了!「或類似的東西),那麼就使用' [^#] +'而不是'[\ w \ s] +'http://ideone.com/zrx9n – polygenelubricants 2010-05-25 14:14:23

+0

如果我保證除了分區分隔符之外的任何行都不會以#開頭,怎麼回事? – sprugman 2010-05-25 14:18:06

3

我會採取一個多步驟的方法:

  • 分成部分的標題/內容
  • 解析每一個標題/內容對成所需的陣列結構

下面是一個例子,拆分成多行,所以你可以跟蹤什麼是要去:

注缺乏理智的C hecking,這假設很好,整潔的標題/內容組。
正則表達式是爲了簡潔而編寫的,可能會或可能不足以滿足您的需求。

// Split string on a line of text wrapped in lines of only #'s 
$parts = preg_split('/^#+$\R(.+)\R^#+$/m', $subject, null, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); 
// Tidy up leading/trailing whitespace for each heading/content-block 
$parts = array_map('trim', $parts); 
// Chunk into array("heading", "content") 
$parts = array_chunk($parts, 2); 

// Create the final array 
$sections = array(); 
foreach ($parts as $part) { 
    $sections[$part[0]] = explode("\n", $part[1]); 
} 

// Lets take a look 
var_dump($sections); 
+0

感謝您的幫助。我結束了與@ polygenelubricants來回...... – sprugman 2010-05-25 15:22:48

+1

Oookkkk。我永遠不會理解這個地方。 : -/ – salathe 2010-05-25 15:54:47