PHP正则表达式 - 从txt获取数组

时间:2014-03-01 21:46:37

标签: php regex arrays preg-match explode

我得到了一个具有以下结构的txt文件:

  

r = data0 n = data2 b =数据3 p = data4 s =数据5 h = data6 t = data7
  r = data8 n =数据9 b = data10 p = data11 s = data12 h = data 13 t = data14
  r = data15 n = data16 b =数据17 p = data18 s = data19 h = data 20 t = data 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);
}
谁能帮助我吗?非常感谢!

招呼

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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
    )

)

答案 2 :(得分: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);