preg_match单词后跟数字

时间:2011-08-18 05:04:41

标签: php preg-match preg-match-all

我有以下字符串:

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;

3 个答案:

答案 0 :(得分:2)

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);
}

答案 1 :(得分: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);

答案 2 :(得分:1)

k102几乎相同的答案略有变化,我正在使用标志PREG_SPLIT_NO_EMPTY,所以在列表的开头不需要$ null var:)

$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);