通过仅输入姓氏,从文件中获取特定操作的计数

时间:2016-03-08 16:11:58

标签: php count get

所以我有一个包含这个的.txt日志:

2015-10-02 11:06:15: P          performed by 'Bruce Wayne' 
2015-1-23 11:06:15: P          performed by 'Bruce Wayne'
2015-11-06 9:06:15: S          performed by 'Peter Parker' 
2015-12-10 10:15:15: SAY          performed by 'Bruce Wayne' 
2015-9-10 10:06:15: SAY          performed by ' Wayne'

他们中的一些人没有名字,而是有一个如上所示的空白。

2015-9-10 10:06:15: SAY          performed by ' Wayne'

如何通过仅输入姓氏来获取人员完成的所有命令的计数?

实施例: 输入: 韦恩

输出: P命令由Wayne完成:3次 SAY命令由Wayne完成:1次(

输入: 帕克

输出: S命令由帕克完成:1次(

我很感激为此提供一些帮助。我一直试着做了一个多星期了。

1 个答案:

答案 0 :(得分:1)

使用preg_match_all

$surname = trim( $surname );

$pattern = "
    /
    ^                           # start of line
    (\d{4}-\d{1,2}-\d{1,2})     # date
    \s+                         # one-or-more spaces
    (\d{1,2}:\d{2}:\d{2}):      # hour
    \s+                         # one-or-more spaces
    (\w+)                       # one-or-more word chars
    \s+                         # one-or-more spaces
    performed\sby\s             # “performed by ”
    '                           # single quote
    ((.*?\s)?{$surname})        # optional ( zero-or-more chars followed by a space ) followed by “$surname”
    '                           # single quote
    /xim
";

preg_match_all( $pattern, $text, $matches );
print_r( $matches );

eval.in demo

单行模式版本:

/^(\d{4}-\d{1,2}-\d{1,2})\s+(\d{1,2}:\d{2}:\d{2}):\s+(\w+)\s+performed\sby\s'((.*?\s)?{$surname})'/im