Solved

PHP: Parsing a block of text properly and getting uptime

Posted on 2010-09-15
5
445 Views
Last Modified: 2013-11-10
I will attach the block of text.

I can get the information at the console, and normally I can parse things using regexp (For example with XML  you can do something like:  "$found = preg_match('#<findthis(?:\s+[^>]+)?>(.*?)'. '</findthis>#s', $result, $matches2);"

I'm having issues though with this one as there is no simple beginning/end..

The line I'm looking to grab is near Uptime  90  0:00:03.. What I want to actually grab in general is "0:00:03"

The issue is however, the text is parsed to be positioned properly. So the text between each area may be different spacing. For example it could be

Uptime  90  0:00:03  .. or maybe:
Uptime  0    0:00:03

Different spacing but same information. I believe its all spaces though. I'm also not 100% sure to 'grab' the 0:00:03 in general too this way.

Does anyone have any ideas? Thanks!

Traffic    ------------inbound------------  ------------outbound-----------
                 rcpts      msgs      kbytes      rcpts      msgs      kbytes
      Total          0         0         0.0          0         0         0.0
  Last Hour          0         0         0.0          0         0         0.0
   Top/Hour          0         0         0.0          0         0         0.0
  Last Min.          0         0         0.0          0         0         0.0
   Top/Min.          0         0         0.0          0         0         0.0

 Connections    active       top     maximum  Domain       cached     pending
    Inbound          0         0          30    Names           0           0
   Outbound          0         0        1200

     Queues      rcpts   domains      kbytes  Spool        in use    recycled
       SMTP          0         0         0.0    Files           0           0
      other          0         0         0.0    Init.                complete

     Status    running    Started  2010-09-16 16:02:08    Uptime  90  0:00:03

Open in new window

0
Comment
Question by:Valleriani
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33685900
You can use this

(?<=Uptime)[\s]*([\d]+[\s]*([\d]{1}|[\d]{2}):[\d]{2}:[\d]{2})

or this (more generic):

(?<=Uptime)[\s]*([\d]+[\s]*[\d]*):[\d]*:[\d]*

Cheers
0
 
LVL 7

Author Comment

by:Valleriani
ID: 33686061
Hey there,

When I am using:

$found = preg_match('(?<=Uptime)[\s]*([\d]+[\s]*([\d]{1}|[\d]{2}):[\d]{2}:[\d]{2})', $result, $matches);


It gives me:


PHP Warning:  preg_match(): Unknown modifier '['
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 250 total points
ID: 33686245
try so

$found = preg_match('/(?<=Uptime)[\s]*([\d]+[\s]*([\d]{1}|[\d]{2}):[\d]{2}:[\d]{2})/', $result, $matches);
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 33689395
Sometimes it is difficult to get a REGEX just right, and an easier solution is to break the string apart with explode(), substr() and strpos() functions.  Try running this little code snippet.  Best, ~Ray
<?php // RAY_temp_valleriani.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA FROM THE POST AT EE
$txt = <<<TXT
Traffic    ------------inbound------------  ------------outbound-----------
                 rcpts      msgs      kbytes      rcpts      msgs      kbytes
      Total          0         0         0.0          0         0         0.0
  Last Hour          0         0         0.0          0         0         0.0
   Top/Hour          0         0         0.0          0         0         0.0
  Last Min.          0         0         0.0          0         0         0.0
   Top/Min.          0         0         0.0          0         0         0.0

 Connections    active       top     maximum  Domain       cached     pending
    Inbound          0         0          30    Names           0           0
   Outbound          0         0        1200

     Queues      rcpts   domains      kbytes  Spool        in use    recycled
       SMTP          0         0         0.0    Files           0           0
      other          0         0         0.0    Init.                complete

     Status    running    Started  2010-09-16 16:02:08    Uptime  90  0:00:03
TXT;

// BREAK THE STRING INTO AN ARRAY
$arr = explode('Uptime', $txt);

// ELIMINATE EXTRANEOUS WHITE SPACE
$txt = trim(preg_replace('/\s\s+/', ' ', $arr[1]));

// FIND THE TIME VALUE
$arr = explode(' ', $txt);
var_dump($arr[1]);

Open in new window

0
 
LVL 7

Author Closing Comment

by:Valleriani
ID: 33689407
Nice solutions guys, thanks!
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question