Parse string for array in PHP

0

I have the following string:

/dev/sda1      ext4      19620732  16936800   1664184
udev           devtmpfs     10240     10240         0
tmpfs          tmpfs       101232     96740      4492
tmpfs          tmpfs       253080    253080         0
tmpfs          tmpfs         5120      5120         0
tmpfs          tmpfs       253080    253080         0
var 89www        vboxsf   487350400 350068644 137281756

I want to pass the string in question to array, so that the final structure is:

Array
(
    [0] => Array
        (
            [sistArq] => /dev/sda1
            [tipo] => ext4
            [tamanho] => 19620732
            [disponivel] => 16936800
            [usado] => 1664184
        )

    [1] => Array
        (
            [sistArq] => udev
            [tipo] => devtmpfs
            [tamanho] => 10240
            [disponivel] => 10240
            [usado] => 0
        )

    [2] => Array
        (
            [sistArq] => tmpfs
            [tipo] => tmpfs
            [tamanho] => 101232
            [disponivel] => 96740
            [usado] => 4492
        )

    [3] => Array
        (
            [sistArq] => tmpfs
            [tipo] => tmpfs
            [tamanho] => 253080
            [disponivel] => 253080
            [usado] => 0
        )

    [4] => Array
        (
            [sistArq] => tmpfs
            [tipo] => tmpfs
            [tamanho] => 5120
            [disponivel] => 5120
            [usado] => 0
        )

    [5] => Array
        (
            [sistArq] => tmpfs
            [tipo] => tmpfs
            [tamanho] => 253080
            [disponivel] => 253080
            [usado] => 0
        )

    [6] => Array
        (
            [sistArq] => var 89www
            [tipo] => vboxsf
            [tamanho] => vboxsf
            [disponivel] => 487350400
            [usado] => 350068644
        )

)

I tried something like:

$output = trim(preg_replace('/^.+\n/', '', $string));
$output = explode("\n", $output);
$output = preg_replace('/[ ]{1,}/', ' == ' ,$output);

for ($i = 0; $i < sizeof($output); $i++)
{
    $campos = explode(" == ", $output[$i]);

    $arr[$i] = array(
                'sistArq' => $campos[0],
                'tipo' => $campos[1],
                'tamanho' => $campos[2],
                'disponivel' => $campos[3],
                'usado' => $campos[4]
                );
}

But I did not have the expected success.

    
asked by anonymous 01.10.2016 / 01:05

2 answers

1

Should work:

$string = "/dev/sda1      ext4      19620732  16936800   1664184\n" .
          "udev           devtmpfs     10240     10240         0\n" .
          "tmpfs          tmpfs       101232     96740      4492\n" .
          "tmpfs          tmpfs       253080    253080         0\n" .
          "tmpfs          tmpfs         5120      5120         0\n" .
          "tmpfs          tmpfs       253080    253080         0\n" .
          "var 89www        vboxsf   487350400 350068644 137281756";

$arr = [];

foreach(preg_split("/((\r?\n)|(\r\n?))/", $string) as $line){
    preg_match("/^([\/\w\d ]+) +([\w\d]+) +(\d+) +(\d+) +(\d+)$/", $line, $campos);

    $arr[] = [
        'sistArq' => trim($campos[1]),
        'tipo' => $campos[2],
        'tamanho' => $campos[3],
        'disponivel' => $campos[4],
        'usado' => $campos[5]
        ];
}

var_dump($arr);
    
01.10.2016 / 08:50
0

I did the reverse logic by taking the array from back to front and when the column limit was equal to 4 , the rest is all of the sistArq key. In this function pass the directory and filename with extension.

Functional sample

Function fetching a variable of type text:

$txt = "/dev/sda1      ext4      19620732  16936800   1664184
        udev           devtmpfs     10240     10240         0
        tmpfs          tmpfs       101232     96740      4492
        tmpfs          tmpfs       253080    253080         0
        tmpfs          tmpfs         5120      5120         0
        tmpfs          tmpfs       253080    253080         0
        var 89www adadfaas       vboxsf   487350400 350068644 137281756";

function parse_file_terminal_linux_string($txt)
{
    $ponteiro = explode("\n", $txt);
    foreach ($ponteiro as $key => $value) 
    {
        $values = explode(" ", $value);     
        $keys = array('usado','disponivel','tamanho','tipo','sistArq'); 
        for($i = count($values); $i >=0 ; $i--)
        {
            if (!empty($values[$i]))
            {
                if (count($keys)>0) 
                    $c = array_shift($keys);                        
                $result[$key][$c] = ($c === 'sistArq' && isset($result[$key][$c])) 
                    ? $result[$key][$c]." ".trim($values[$i])
                    : trim($values[$i]);
            }
        }
        $result[$key]['sistArq'] =
            implode(" ", array_reverse(explode(" ",$result[$key]['sistArq'])));
    }   
    return $result; 
}

Function fetching a text file:

function parse_file_terminal_linux($name = "arq.txt")
{
    $ponteiro = file($name);
    foreach ($ponteiro as $key => $value) 
    {
        $values = explode(" ", $value);     
        $keys = array('usado','disponivel','tamanho','tipo','sistArq'); 
        for($i = count($values); $i >=0 ; $i--)
        {
            if (!empty($values[$i]))
            {
                if (count($keys)>0) 
                    $c = array_shift($keys);                        
                $result[$key][$c] = ($c === 'sistArq' && isset($result[$key][$c])) 
                    ? $result[$key][$c]." ".trim($values[$i])
                    : trim($values[$i]);
            }
        }
        $result[$key]['sistArq'] =
            implode(" ", array_reverse(explode(" ",$result[$key]['sistArq'])));
    }   
    return $result; 
}

print_r(parse_file_terminal_linux("files/arq.txt"));
    
01.10.2016 / 06:56