Link to home
Start Free TrialLog in
Avatar of Valleriani
VallerianiFlag for Sweden

asked on

PHP: Parsing a block of text properly and getting uptime

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

Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

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
Avatar of Valleriani

ASKER

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 '['
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice solutions guys, thanks!