Jump to content


Photo

Newbie question: Extracting text from a string and storing in a variable


  • Please log in to reply
2 replies to this topic

#1 gregp

gregp
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 19 September 2006 - 01:07 AM

Hi guys,

I'm fairly new to PHP which the question probably suggests! :) I'm sure this is not difficult at all but I'm obviously missing one step.

I have a script which examines a log file and generates HTML based on the information in that logfile. The start of the log file looks as follows:

# NetSaint 0.0.7b7 Status File
[1158624594] PROGRAM;1158131198;18534;0;ACTIVE;1158131199;1158624579;1158417000;1;1;1;0;0
[1158624594] HOST;aarnet;UP;1158580409;1158448819;0;317427;189;0;0;0;1;1;1;1;0;0.00;0;PING OK - Packet loss = 0%, RTA = 0.57 ms

To get the status for aarnet for example, I store HOST;aarnet;UP in a variable such as aarnet_OK and use the following code to check whether there's a match anywhere in the line presently under review, and to set a flag if it finds it:
if (strpos($line, $aarnet_OK))  {
$aarnet_status = 'OK';
}

to get the status flag. That works, though it's a bit messy. I would prefer to extract the text 'up' from the logfile and use that.

The situation now is that I want to display the datestamp of the logfile, which is the string of characters in the first line. Obviously this will change every minute so I can't use the above code to do that - I need to do exactly what I originally wanted, that is, extract the 10 digit character and store it in a variable.

Incidentally this same string appears at the start of every of the 400 lines in the logfile so it would be nice to be able to specify just one line to do this extraction on rather than running it on every single line of the log file. The code above is within the following code:
$fp = fopen('status.log', 'r');
$strings = array();
while(!feof($fp)){
$line = fgets($fp);

Can someone please, after groaning at how simple this request is, show me what code I should use for that? I figure it may be preg_match or something along those lines but haven't had any suggess so far.

Thanks in anticipation!

Greg

#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 19 September 2006 - 01:35 AM

preg_match() will certainly work.  You could also use substr().

Since the value you want is a fixed length at a fixed offset, you can use

$timestamp = substr($line, 1, 10);

If you want to use preg_match():

if (preg_match('/^\[([[:digit:]]*)\]/', $line, &$matches)) { $timestamp = $matches[1]; }

might work, providing I haven't made any mistakes.

PS, when using strpos() you should check "if (strpos($line, $aarnet_OK) !== false)" instead.  For your case it will work, but you may have trouble later.  See http://www.php.net/m...tion.strpos.php

#3 gregp

gregp
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 19 September 2006 - 03:11 AM

Thanks! :) Knew it would be something pretty straightforward. Went with the substr option and works a treat!

Incidentally there are several different strings the log file may contain and these are in an if else loop with a generic message in the else statement at the end to catch anything unexpected. Thanks for your time!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users