解析php中的ip地址

时间:2013-10-30 19:14:49

标签: php parsing

我正在尝试通过IP连接并具有播放状态从日志文件中获取活动连接数(实时),而是通过状态播放向我提供IP总数。这个数字根本没有减少。一旦添加新的ip,就会继续增加。我该如何解决?这是我的代码:

$stringToParse = file_get_contents('wowzamediaserver_access.log');
preg_match_all('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $stringToParse, $matchOP);
echo "Number of connections: ".sizeof(array_unique($matchOP[0]));

这是日志:

2013-10-30  14:54:36    CET stop    stream  INFO    200 account1    -   _defaultVHost_  account1    _definst_   149.21  streamURL   1935    fullStreamURL   IP_ADDRESS_1    http (cupertino)    -   
2013-10-30  14:56:12    CET play    stream  INFO    200 account2    -   _defaultVHost_  account1    _definst_   149.21  streamURL   1935    fullStreamURL   IP_ADDRESS_2    rtmp (cupertino)    -   
2013-10-30  14:58:23    CET stop    stream  INFO    200 account2    -   _defaultVHost_  account1    _definst_   149.21  streamURL   1935    fullStreamURL   IP_ADDRESS_2    rtmp (cupertino)    -   
2013-10-30  14:58:39    CET play    stream  INFO    200 account1    -   _defaultVHost_  account1    _definst_   149.21  streamURL   1935    fullStreamURL   IP_ADDRESS_1    http (cupertino)    -
2013-10-30  14:59:12    CET play    stream  INFO    200 account2    -   _defaultVHost_  account1    _definst_   149.21  streamURL   1935    fullStreamURL   IP_ADDRESS_2    rtmp (cupertino)    -

我希望能够在IP处于“PLAY”状态时对其进行计数,并且只要它处于“STOP”状态就不计算IP

    2013-10-30  14:59:00    CET play    stream  INFO    200 tv2vielive  -   _defaultVHost_  tv2vielive  _definst_   0.315   [any]   1935    rtmp://tv2vie.zion3cloud.com:1935/tv2vielive    78.247.255.186  rtmp    http://www.tv2vie.org/swf/flowplayer-3.2.16.swf WIN 11,7,700,202    92565864    3576    3455    1   0   0   0   tv2vielive  -   -   -   -   -   rtmp://tv2vie.zion3cloud.com:1935/tv2vielive/tv2vielive rtmp://tv2vie.zion3cloud.com:1935/tv2vielive/tv2vielive -   rtmp://tv2vie.zion3cloud.com:1935/tv2vielive    -


2013-10-30  14:59:04    CET stop    stream  INFO    200 tv2vielive  -   _defaultVHost_  tv2vielive  _definst_   4.75    [any]   1935    rtmp://tv2vie.zion3cloud.com:1935/tv2vielive    78.247.255.186  rtmp    http://www.tv2vie.org/swf/flowplayer-3.2.16.swf WIN 11,7,700,202    92565864    3576    512571  1   7222    0   503766  tv2vielive  -   -   -   -   -   rtmp://tv2vie.zion3cloud.com:1935/tv2vielive/tv2vielive rtmp://tv2vie.zion3cloud.com:1935/tv2vielive/tv2vielive -   rtmp://tv2vie.zion3cloud.com:1935/tv2vielive    -

任何解决方案?我甚至尝试了第一个答案解决方案,但获得了“0”播放连接。

$stringToParse = file_get_contents('wowzamediaserver_access.log');

$pattern = '~^.* play.* ( ([0-9]{1,3}+\.){3,3}[0-9]{1,3}).*$~m';
preg_match_all($pattern, $stringToParse, $matches);

echo count($matches[1]) . ' play actions';

但每当我使用我的代码时:

$stringToParse = file_get_contents('wowzamediaserver_access.log');
preg_match_all('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $stringToParse, $matchOP);
echo "Number of connections: ".sizeof(array_unique($matchOP[0]));

我得到“连接数:xxxxx(IP的实际数量)。 我担心的是我只需要具有PLAY状态的IP数量。如果该IP更改为STOP,则它不会计数。

1 个答案:

答案 0 :(得分:0)

试试这个:

$pattern = '~^.* play.* (([0-9]{1,3}\.){3}[0-9]{1,3}).*$~m';
preg_match_all($pattern, $stringToParse, $matches);

echo count($matches[1]) . ' play actions';

它将提取包含文字play的那些行的IP地址并计算它们。