Solved

PHP: Parsing a block of text properly and getting uptime

Posted on 2010-09-15
5
435 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
  • 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

914 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now