Solved

PHP: Parsing a block of text properly and getting uptime

Posted on 2010-09-15
5
430 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 30

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 30

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
htacces issue 2 39
Printing unique/distinct values PHP, MSSQL, Codeigniter 58 89
Time difference 10 35
.htaccess file settings 4 34
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 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

16 Experts available now in Live!

Get 1:1 Help Now