Preg_match单词后跟数字


preg_match words followed by numbers

我有以下字符串:

Query_time: 77.571216 Lock_time: 2.793139 Rows_sent: 20 Rows_examined: 4654071

如何将每个数字放入变量中?如:

$query_time    = 77.571216;  
$lock_time     = 2.793139;
$rows_sent     = 20;
$rows_examined = 4654071;
if (preg_match('/Query_time: ([0-9.]+) Lock_time: ([0-9.]+) Rows_sent: ([0-9.]+) Rows_examined: ([0-9.]+)/', $string, $matches)) {
    list($query_time, $lock_time, $rows_sent, $rows_examined) =
            array_slice($matches, 1);
}
$str = 'Query_time: 77.571216 Lock_time: 2.793139 Rows_sent: 20 Rows_examined: 4654071';
list($null, $query_time, $lock_time, $rows_sent, $rows_examined) = preg_split('/'S+:/',$str);

与k102的答案几乎相同,只是有一点变化,我使用PREG_SPLIT_NO_EMPTY标志,因此不需要在列表开头使用$null变量:)

$str = 'Query_time: 77.571216 Lock_time: 2.793139 Rows_sent: 20 Rows_examined: 4654071';
list($query_time, $lock_time, $rows_sent, $rows_examined) = preg_split('/'S+:/',$str, 0, PREG_SPLIT_NO_EMPTY);