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]
);
}
However, I did not have the expected success.
In this case it seems easier to use substring than Regex. Incidentally, the columns may not be fixed depending on the size of the information listed, but it would be the case to take a look at the command flags to see which ones affect the output formatting (for example, force Posix format, these things)
– Bacco
the columns are separated by a tab? if it is he only read each row with a explode n or r and in each row you give a explode to the tab
– Jasar Orion
detail the generation of that file in your question?
– novic
This output comes from the Linux terminal, the columns are separated by space. Since the first column has several spaces as separator, the others can have only 1 space or several sequentially
– Fábio Jânio