Hello friends

I have little issue.
I'm having a block of text, which is log from syslog.

say log is

Oct 28 11:42:59 MyMachine dbus[692]: [system] Activating service name='net.reactivated.Fprint' (using servicehelper)
Oct 28 11:42:59 MyMachine dbus-daemon[692]: dbus[692]: [system] Successfully activated service 'net.reactivated.Fprint'
Oct 28 11:42:59 MyMachine dbus[692]: [system] Successfully activated service 'net.reactivated.Fprint'
Oct 28 11:42:59 MyMachine dbus-daemon[692]: Launching FprintObject
Oct 28 11:42:59 MyMachine dbus-daemon[692]: ** Message: D-Bus service launched with name: net.reactivated.Fprint
Oct 28 11:42:59 MyMachine dbus-daemon[692]: ** Message: entering main loop
Oct 28 11:43:29 MyMachine dbus-daemon[692]: ** Message: No devices in use, exit
Oct 28 12:31:30 MyMachine kernel: [44913.774396] perf samples too long (2506 > 2500), lowering kernel.perf_event_max_sample_rate to 50000
Oct 28 13:12:15 MyMachine dbus-daemon[692]: dbus[692]: [system] Activating service name='net.reactivated.Fprint' (using servicehelper)
Oct 28 13:12:15 MyMachine dbus[692]: [system] Activating service name='net.reactivated.Fprint' (using servicehelper)
Oct 28 13:12:15 MyMachine dbus-daemon[692]: dbus[692]: [system] Successfully activated service 'net.reactivated.Fprint'
Oct 28 13:12:15 MyMachine dbus[692]: [system] Successfully activated service 'net.reactivated.Fprint'
Oct 28 13:12:15 MyMachine dbus-daemon[692]: Launching FprintObject
Oct 28 13:12:15 MyMachine dbus-daemon[692]: ** Message: D-Bus service launched with name: net.reactivated.Fprint
Oct 28 13:12:15 MyMachine dbus-daemon[692]: ** Message: entering main loop
Oct 28 13:12:45 MyMachine dbus-daemon[692]: ** Message: No devices in use, exit
Oct 28 14:21:16 MyMachine systemd[1]: Started Sendmail Mail Transport Agent.
Oct 28 14:36:18 MyMachine avahi-daemon[686]: Invalid legacy unicast query packet.

Now I want to read this log file line by line. let me tell you some of log is spanned in multiple line, so it would be better to say that I want to read logs sentence by sentence.
And hence I applied following preg_split regex.

if( $matches = preg_split("/^[a-zA-Z]{3} [0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9] /",$logBuffer, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE)) {
                echo "We think we found something\n";
                print_r($matches);
        }

But what I am getting is everything in one array index.
Can anyone suggest what am I doing wrong here?
Basic idea is to split the log based on simple regex "Oct(3 char) 28(2 char) 14:36:18 (few more chars in these fashion)".

Thanks
Hemanshu

Hi

 if( $matches = preg_split("/[a-zA-Z]{3} [0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9] /",$json, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE)) {
                echo "We think we found something\n";
                print_r($matches);
        }

Has done the trick, still I am not getting the starting part as well.

I mean i want to get Oct 23 11:11:11 as well along with the string.
Any Idea, fellas?

Hemanshu

Well It probably is a bad hack.
but i found something with which It worked.

        if( $matches = preg_split("/[a-zA-Z]{3} [0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9] /",$json, -1, PREG_SPLIT_OFFSET_CAPTURE)) {
                echo "We think we found something\n";
                $i=1;
                while(isset($matches[$i])) {
                        $matches[$i][0]=substr($json,$matches[$i][1]-16,16).$matches[$i][0];
                        $i++;
                }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.