Solved

PHP: Parsing a block of text properly and getting uptime

Posted on 2010-09-15
5
437 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 109

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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…

825 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