4
Total accesses: 296282 - Total Traffic: 1.2 GB
CPU Usage: u757.94 s165.33 cu0 cs0 - 2.56% CPU load
8.22 requests/sec - 33.5 kB/second - 4175 B/request
22 requests currently being processed, 26 idle workers

假设我们将上述内容作为字符串,并将该字符串存储在变量中。问题:我想获得以下值:

  1. 请求/秒 - 当前值为 8.22
  2. 当前正在处理的请求 - 当前值为 22
  3. 闲置工人 - 当前值为 26

使用strpossubstr完成了一些解决方案,但我认为这根本不是一个好的解决方案,因为上面的这些值是动态的,它可能是 2 位或 4 位或更长。

示例代码:

$rps = substr($data,strpos($data,"requests/sec")-5,4);
echo $rps; //displays 8.22

还有另一种方法可以做到这一点吗?我将不胜感激任何回应:)

4

3 回答 3

4

常用表达。

$values = array();
preg_match("/(?P<rps>[\d.]+) requests\/sec(.*)(?P<requests>[\d]+) requests (.*)(?P<workers>[\d]+) idle workers/", $your_text, $values);

print 
"requests/sec - CURRENT VALUE IS {$values['rps']}
 requests currently being processed - CURRENT VALUE IS {$values['requests']}
 idle workers - CURRENT VALUE IS {$values['workers']}";
于 2012-07-17T15:47:43.923 回答
3

正则表达式可以工作:

<?php
$data = 'Total accesses: 296282 - Total Traffic: 1.2 GB
CPU Usage: u757.94 s165.33 cu0 cs0 - 2.56% CPU load
8.22 requests/sec - 33.5 kB/second - 4175 B/request
22 requests currently being processed, 26 idle workers';

if( preg_match('/(\d*\.?\d*)\srequests\/sec/', $data, $m) ){
    echo $m[1];
}else{
    echo 'no match';
}
于 2012-07-17T15:45:54.960 回答
2

假设所有输出都在字符串变量 $output 中。你可以这样做:

// requests per second
$regex = '#^([0-9]*\.?[0-9].*)\040requests/sec#';
preg_match($regex, $output, $matches);
$req_per_sec = $matches[1];

// request being processed
$regex = '/([0-9]*)\040requests\040currently/';
preg_match($regex, $output, $matches);
$req_processed = $matches[1];

// idle workers
$regex = '/\040([0-9]*)\040idle\040workers/';
preg_match($regex, $output, $matches);
$idle_workers = $matches[1];
于 2012-07-17T15:57:57.257 回答