我正在尝试在我使用运行的 Linux 命令的输出上运行一些正则表达式exec()
。将输出作为干草堆插入时preg_match
返回,但是当我将此输出的相同副本放入字符串中并将其用作干草堆时,工作正常并输出预期的数组。NULL
exec()
preg_match
代码:
exec("/usr/local/bin/ffmpeg -i video.flv 2>&1 | grep Duration", $output);
//$output[0] will be: "Duration: 00:01:00.08, start: 0.042000, bitrate: 164 kb/s"
$string = $output[0];
//This regex is to extract the duration, "00:01:00", from the output.
$pattern = "/(^Duration:) ([^&]+)(.[0-9][0-9])(, start)/";
preg_match($pattern, $string, $matches);
print_r($matches[2]);
//The result should be "00:01:00" but the whole $matches array is empty instead.
//If I added the result of output[0] manually into a string it works fine.
$str = "Duration: 00:01:00.08, start: 0.042000, bitrate: 164 kb/s";
preg_match($pattern, $str, $matches_);
print_r($matches_[2]);
//Outputs "00:01:00" and everything is working fine.
//Just to make sure, I printed $str and $string to see if there is any difference
echo $str."\n".$string;
//The output is:
//Duration: 00:01:00.08, start: 0.042000, bitrate: 164 kb/s
//Duration: 00:01:00.08, start: 0.042000, bitrate: 164 kb/s