0

i got a txt file with the following structure:

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

i would have an array like this:

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

for each row an array ... my code (i tried to explode the lines first - this worked, after that i would like to use a regex, didn't worked :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);
}

can anyone help me? thanks a lot!

greets

0

3 Answers 3

2

You can use this:

$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);

an other way:

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

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

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

$result = json_decode($json, true);
Sign up to request clarification or add additional context in comments.

Comments

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];
         }
     }
 }

result:

 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
    )

)

Comments

0

Not as elegant as @Casimir but I think this should work too.

$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.