Jump to content


This topic is now archived and is closed to further replies.


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

Recommended Posts

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();
$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!


Share this post

Link to post
Share on other sites
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

[code]$timestamp = substr($line, 1, 10);[/code]

If you want to use preg_match():

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

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/manual/en/function.strpos.php

Share this post

Link to post
Share on other sites
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!

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.