Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6477217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:55:17+00:00 2026-05-25T06:55:17+00:00

Our application receives log files via email and so the lines are often broken

  • 0

Our application receives log files via email and so the lines are often broken up by the email client. Once I’ve read the body of the email in I have a string variable $log in the following format.

Fri Aug 26 11:52:30 2011 OpenVPN 2.1.4 i686-pc-mingw32 [SSL] [LZO2] 
PKCS11] built Fri Aug 26 11:52:30 2011 NOTE: OpenVPN 2.1 requires '--script-security 2' 
or higher to call user-defined scripts or executables Fri Aug 26 11:52:30 2011 
Control Channel Authentication: using 'ta.key' as a OpenVPN static key file 
Fri Aug 26 11:52:30 2011 Outgoing Control Channel Authentication: Using 160 
bit message hash 'SHA1' for HMAC authentication Fri Aug 26 11:52:30 
2011 Incoming Control Channel Authentication: Using 160 bit message hash 'SHA1'
for HMAC authentication Fri Aug 26 11:52:30 2011 LZO compression initialized 
Fri Aug 26 11:52:30 2011 Control Channel MTU parms [ L:1558 D:166 EF:66 EB:0 
ET:0 EL:0 ] Fri Aug 26 11:52:30 2011 Socket Buffers: R=[8192->8192] S=[8192->8192]

As shown above the date does not always start on a newline. I’d like to generate an array containing the dates and log messages so that I can output a table with these fields in their own columns. I understand that I would need a regex to match the date field but how do I go about building the array?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:55:17+00:00Added an answer on May 25, 2026 at 6:55 am

    I’m just going to update my answer with a new version entirely, since the example log file has changed a lot. Since the log seems to be line broken just about anywhere, this approach – now including a bit of regexp works:

    $log="Fri Aug 26 11:52:30 2011 OpenVPN 2.1.4 i686-pc-mingw32 [SSL] [LZO2]  
    PKCS11] built Fri Aug 26 11:52:30 2011 NOTE: OpenVPN 2.1 requires '--script-security 2'  
    or higher to call user-defined scripts or executables Fri Aug 26 11:52:30 2011  
    Control Channel Authentication: using 'ta.key' as a OpenVPN static key file  
    Fri Aug 26 11:52:30 2011 Outgoing Control Channel Authentication: Using 160  
    bit message hash 'SHA1' for HMAC authentication Fri Aug 26 11:52:30  
    2011 Incoming Control Channel Authentication: Using 160 bit message hash 'SHA1' 
    for HMAC authentication Fri Aug 26 11:52:30 2011 LZO compression initialized  
    Fri Aug 26 11:52:30 2011 Control Channel MTU parms [ L:1558 D:166 EF:66 EB:0  
    ET:0 EL:0 ] Fri Aug 26 11:52:30 2011 Socket Buffers: R=[8192->8192] S=[8192->8192] 
    ";
    $str = implode(' ',preg_split("/[ ]*[\r\n]+/", $log));
    $arrLogLines=preg_split('/[ ]*([\w]{3} [\w]{3} [0-9]{2} [\d:]+ \d{4}) /',$str,-1,PREG_SPLIT_DELIM_CAPTURE); // Cred to Herbert for the regexp, seems to work fine..
    array_shift($arrLogLines);
    for ($i=0;$i<sizeof($arrLogLines);$i++) {
        if (($i/2)==(int)($i/2)) {
            $offset=0;
            $strArrIdx='date';
        } else {
            $offset=1;
            $strArrIdx='message';
        }
        $arrLogMessages[($i-$offset)/2][$strArrIdx]=$arrLogLines[$i];
    }
    var_dump($arrLogMessages);
    

    It produces the expected:

    array(8) {
      [0]=>
      array(2) {
        ["date"]=>
        string(24) "Fri Aug 26 11:52:30 2011"
        ["message"]=>
        string(56) "OpenVPN 2.1.4 i686-pc-mingw32 [SSL] [LZO2] PKCS11] built"
      }
      [1]=>
      array(2) {
        ["date"]=>
        string(24) "Fri Aug 26 11:52:30 2011"
        ["message"]=>
        string(102) "NOTE: OpenVPN 2.1 requires '--script-security 2' or higher to call user-defined scripts or executables"
      }
      [2]=>
      array(2) {
        ["date"]=>
        string(24) "Fri Aug 26 11:52:30 2011"
        ["message"]=>
        string(75) "Control Channel Authentication: using 'ta.key' as a OpenVPN static key file"
      }
      [3]=>
      array(2) {
        ["date"]=>
        string(24) "Fri Aug 26 11:52:30 2011"
        ["message"]=>
        string(98) "Outgoing Control Channel Authentication: Using 160 bit message hash 'SHA1' for HMAC authentication"
      }
      [4]=>
      array(2) {
        ["date"]=>
        string(24) "Fri Aug 26 11:52:30 2011"
        ["message"]=>
        string(98) "Incoming Control Channel Authentication: Using 160 bit message hash 'SHA1' for HMAC authentication"
      }
      [5]=>
      array(2) {
        ["date"]=>
        string(24) "Fri Aug 26 11:52:30 2011"
        ["message"]=>
        string(27) "LZO compression initialized"
      }
      [6]=>
      array(2) {
        ["date"]=>
        string(24) "Fri Aug 26 11:52:30 2011"
        ["message"]=>
        string(63) "Control Channel MTU parms [ L:1558 D:166 EF:66 EB:0 ET:0 EL:0 ]"
      }
      [7]=>
      array(2) {
        ["date"]=>
        string(24) "Fri Aug 26 11:52:30 2011"
        ["message"]=>
        string(46) "Socket Buffers: R=[8192->8192] S=[8192->8192] "
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In our application we have an object that receives attributes at runtime. For example,
In our application, we receive text files ( .txt , .csv , etc.) from
Our application is interfacing with a lot of web services these days. We have
Our application is well structured (well we did our best!) and we have split
Our application is written in C++ and used on Windows XP. On some client
Our application runs off MySQL, but a client has another application that requires SQL.
So we've produced a windows service to feed data to our client application and
I have a question regarding handling errors in a J2EE application. Our current application
I have an application that receives information from a database, and is used to
Background We have a Windows .NET application used by our field employees who travel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.