2014-03-01 28 views
0

我得到具有以下結構的txt文件得到數組:PHP正則表達式從TXT

R = DATA0 N = DATA2 B =數據3 P = DATA4 S =數據5小時= DATA6噸= DATA7
r = data8 n =數據9 b = data10 p = data11 s = data12 h = data13 t = data14
r = data15 n = data16 b =數據17 p =數據18 s =數據19 h =數據20 t =數據21

我將有一個這樣的陣列:

array { 
    [r]=> "data0" 
    [n]=> "data2" 
    [b]=> "data 3" 
    [p]=> "data4" 
    [s]=> "data 5" 
    [h]=> "data6" 
    [t]=> "data7" 
} 

每個行的一個數組...我的代碼(我想第一爆線 - 這工作,從那以後,我想用正則表達式,沒有工作:d):

$data = file_get_contents("../data/file.txt"); 

$data = explode("\n", $data); 

foreach($data as $line) { 
    preg_match("/([a-z]=[^=]*\s*)*/", $line, $hit); 
    var_dump($hit); 
} 

任何人都可以幫助我嗎?非常感謝!

招呼

回答

2

您可以使用此:

$data = file_get_contents("../data/file.txt"); 
$lines = explode("\n", $data); 
$result = array(); 
foreach ($lines as $line) { 
    preg_match_all('~([a-z])=\s*(.+?)\s*(?=[a-z]=|$)~', $line, $matches, PREG_SET_ORDER); 
    $tmp = array(); 
    foreach ($matches as $m) { 
     $tmp[$m[1]] = $m[2]; 
    } 
    $result[] = $tmp; 
} 
print_r($result); 

的另一種方式:

$json = preg_replace('~ +(?=[a-z]=)~', '","', $data); 

$trans = array('= ' => '":"', 
       "\n" => '"},{"'); 

$json = '[{"' . strtr($json, $trans) . '"}]'; 

$result = json_decode($json, true); 
+0

非常感謝,非常好的解決方案! – Aaroniker

0

不一樣優雅的@Casimir,但我認爲這應該工作了。

$data = file_get_contents("../data/file.txt"); 
$data = explode("\n", $data); 

//should be an array of arrays 
$finalArray = array(); 

foreach($data as $line) { 
    $arrayWithData = array(); 
    $data_split = explode("=", $data); 
    $currKey = null; 
    foreach ($data_split as $currData) { 
     //if there is just one letter, we know its a key but this is an assumption 
     if (strlen($currData) == 1)) { 
      $currKey = $currData; 
     //if not, must be data so lets check if its in the finalArray 
     } else if (array_key_exists($currData, $finalArray)) { 
      $arrayWithData[$currKey] = trim($currData); 
     } 
    } 
    array_push($finalArray, $arrayWithData); 
} 
print_r("<pre>" . $finalArray . "</pre">, true); 
1
// your file data for example test.txt; 
r= data0 n= data2 b= data 3 p= data4 s= data 5 h= data6 t= data7 
r= data8 n= data 9 b= data10 p= data11 s= data12 h= data13 t= data14 
r= data15 n= data16 b= data 17 p= data18 s= data19 h= data 20 t= data 21'; 

//////////////////////////////////// 

$array = file('test.txt'); 

$result = array(); 
foreach($array as $key => $value){ 
    preg_match_all('/[a-z]{1}\=\s+[a-zA-Z0-9]+\s*[0-9]*/', $value, $matches); 
    foreach($matches as $k => $v){ 
     foreach($v as $vk => $vv){ 
      $exploded = explode('=',$vv); 
      $result[$key][$exploded[0]] = $exploded[1]; 
     } 
    } 
} 

結果:

Array 
(
[0] => Array 
    (
     [r] => data0 
     [n] => data2 
     [b] => data 3 
     [p] => data4 
     [s] => data 5 
     [h] => data6 
     [t] => data7 

    ) 

[1] => Array 
    (
     [r] => data8 
     [n] => data 9 
     [b] => data10 
     [p] => data11 
     [s] => data12 
     [h] => data13 
     [t] => data14 

    ) 

[2] => Array 
    (
     [r] => data15 
     [n] => data16 
     [b] => data 17 
     [p] => data18 
     [s] => data19 
     [h] => data 20 
     [t] => data 21 
    ) 

)