<?php
function extrairDados($dados) {
if (preg_match_all('/Stream\s[#](\d+:\d+)(\(\w+\))?:(\s\w+|)[ :]+(\w+|)/i', $dados, $output) == 0) { echo 'Nenhuma informação encontrada', PHP_EOL;
} else {
$reorganizado = array(); //Array que terá o resultado final
//Chaves que serão usadas para tornar mais intuitivo o que é cada item
'tempo',
'idioma',
'formato',
'codec'
);
//Remove o primeiro item do array gerado pelo preg_match_all, ele não é necessário
//Conta o total de itens
for ($x = 0; $x < $y; $x++) {
$item = $output[$x]; //Pega o item atual
$chave = $chaves[$x]; //Pega a chave atual para identificar no array
$j = count($item); //Conta "propriedades" do item
for ($i = 0; $i < $j; $i++) {
//Se não existir o sub-array irá gerar
if (isset($reorganizado[$i]) === false) { $reorganizado[$i] = array(); }
$str = trim($item[$i]); //Remove espaços em branco $str = trim($str, '('); //Remove ( das extremidades $str = trim($str, ')'); //Remove ) das extremidades
//Salva o item no array chave correspondente
$reorganizado[$i][$chave] = $str;
}
}
//Exibe o array
return $reorganizado;
}
return false;
}
$resposta1 = ' Stream #0:0(und): Video: h264 (High) ...
Stream #0:1(und): Audio: aac (LC) ...';
$resposta2 = ' Stream #0:0: Video: mpeg4 ...
Stream #0:1: Audio: mp3 ...';
$resposta3 = ' Stream #0:0(und): Video: mpeg4 ...
Stream #0:1(jpn): Audio: mp3 ...
Stream #0:1(por): Subtitle:';
PD9waHAKCmZ1bmN0aW9uIGV4dHJhaXJEYWRvcygkZGFkb3MpIHsKICAgIGlmIChwcmVnX21hdGNoX2FsbCgnL1N0cmVhbVxzWyNdKFxkKzpcZCspKFwoXHcrXCkpPzooXHNcdyt8KVsgOl0rKFx3K3wpL2knLCAkZGFkb3MsICRvdXRwdXQpID09IDApIHsKICAgICAgICBlY2hvICdOZW5odW1hIGluZm9ybWHDp8OjbyBlbmNvbnRyYWRhJywgUEhQX0VPTDsKICAgIH0gZWxzZSB7CiAgICAgICAgJHJlb3JnYW5pemFkbyA9IGFycmF5KCk7IC8vQXJyYXkgcXVlIH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