Home > Backend Development > PHP Tutorial > PHP正则解析|提取|过滤标准的syslog日志文件内容,该怎么解决

PHP正则解析|提取|过滤标准的syslog日志文件内容,该怎么解决

WBOY
Release: 2016-06-13 11:48:06
Original
985 people have browsed it

PHP正则解析|提取|过滤标准的syslog日志文件内容
日志内容:
Dec 30 15:10:48 root my: 192.168.1.51 test 退出邮件管理系统
Dec 30 15:11:23 root my: 192.168.1.51 stella 退出邮件管理系统
...
通过正则逐行提取有用信息,并返回数组
...
解析后:
array(
 [0]=>array(
    [0]=>Dec 30 15:10:48,
    [1]=>root,
    [2]=>my,
    [3]=>192.168.1.51,
    [4]=>test,
    [5]=>退出邮件管理系统
  ),
 [1]=>array(
    [0]=>Dec 30 15:11:23,
    [1]=>root,
    [2]=>my,
    [3]=>192.168.1.51,
    [4]=>stella ,
    [5]=>退出邮件管理系统
  ),
);

求,解析过程
------解决方案--------------------
分析日志文件不宜使用正则表达式(效率太低)

$fn = 'sys.log';<br />$fp = fopen($fn, 'r');<br />while(! feof($fp)) {<br />  $r[] = fscanf($fp, "%s %s %s %s my: %s %s %s\n");<br />}<br />print_r($r);
Copy after login
Array
(
    [0] => Array
        (
            [0] => Dec
            [1] => 30
            [2] => 15:10:48
            [3] => root
            [4] => 192.168.1.51
            [5] => test
            [6] => 退出邮件管理系统
        )

    [1] => Array
        (
            [0] => Dec
            [1] => 30
            [2] => 15:11:23
            [3] => root
            [4] => 192.168.1.51
            [5] => stella
            [6] => 退出邮件管理系统
        )

)

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template