Pav
Pav

Reputation: 2328

preg_match words followed by numbers

I've got the following string:

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

How would i go about placing each number into a variable? eg:

$query_time    = 77.571216;  
$lock_time     = 2.793139;
$rows_sent     = 20;
$rows_examined = 4654071;

Upvotes: 0

Views: 1279

Answers (3)

Pedro Lobito
Pedro Lobito

Reputation: 98961

Almost the same answer as k102 with a slight change, I'm using the flag PREG_SPLIT_NO_EMPTY, so there's no need to have a $null var at the beginning of the list :)

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

Upvotes: 1

k102
k102

Reputation: 8079

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

Upvotes: 1

Phil
Phil

Reputation: 164892

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

Upvotes: 2

Related Questions