Jump to content

[SOLVED] Filtering content from a file


monkeypaw201

Recommended Posts

I was looking for a way to filter content out of a file..

 

There is a file formatted with something like this:

 

;comments

;

!STATS:

A:B:C

!SOMETHING:

A:B:C

!STATS:

A:B:C

 

The problem is that I need to retrieve the lines (# varies) in the section right after !SOMETHING: and up to !STATS: ..

 

Any advice on how to tackle the problem?

Link to comment
Share on other sites

corbin, thanks for the fast response... i took a look at the strpos() function but don't really understand how its going to help me.. it would just tell me if the string is there or not right?

 

It may just be that its late.. but i don't get it.. if you could be so kind as to provide some more information my brain would greatly appreciate it

Link to comment
Share on other sites

Two ways to do with, regex or strpos.

 

strpos is faster, So i'll use that.

 

<?php

$file = 'contents.txt';
$start = '!SOMETHING:';
$end = '!STATS:';

# Put file to an array
$lines = file( $file );

# Set up placeholders
$in = FALSE;
$filtered = array();
$i = 0;

# Loop through lines
foreach( $lines as $line ) {

# Check to see if catpuring is on
if ( $in === TRUE ) {
	# Check to see if this is the termination line
	if ( strpos($line, $end) === 0 )
		# Terminate capturing
		$in = FALSE;
	else
		# Append line to filtered output
		$filtered[$i] .= $line;

# Check to see if this is an opening line
} elseif ( strpos($line, $start) === 0 ) {
	# Turn on capturing
	$in = TRUE;
	# Increment counter
	$i++;
	# Create an empty string ( error prevention )
	$filtered[$i] = '';
}

}

# Output results
print_r( $filtered );

?>

 

This is a basic solution, and assumes proper syntax is used. It doesn'tcheck for nested starting strings, nor does it verify there's an ending string before capturing results. Hope this helps.

Link to comment
Share on other sites

discomatt, that works but it seems to dump everything into a single array? how would I run run a string explode on the array? (is it possible)

 

I assume it would look something like this..

 

//The code you gave me is above
$clients = explode(":", $filtered[1]);

//Then I take that array, insert into a database on a loop

 

EDIT:

Also I just realized, that by doing the stropos, it elimiates the line breaks that used to be there..

Link to comment
Share on other sites

sasa - I don't believe this situation warrants the use of regex... The syntax is fairly tight, and regex is extremely slow compared to PHP's basic string functions.

 

discomatt, that works but it seems to dump everything into a single array? how would I run run a string explode on the array? (is it possible)

 

I assume it would look something like this..

 

//The code you gave me is above
$clients = explode(":", $filtered[1]);

//Then I take that array, insert into a database on a loop

 

EDIT:

Also I just realized, that by doing the stropos, it elimiates the line breaks that used to be there..

 

No, the line breaks will still be there. My example was based on what you asked for. Please provide me with a more detailed explanation, or several examples of sample input data and how you would like the data to be returned.

Link to comment
Share on other sites

sasa, thanks for your help.. i tried modifying to retrieve from a file,

 

<?php

$file = 'source.txt';

# Put file to an array
$lines = file( $file );

$out = preg_match('/(?<=!CLIENTS:\n).*?(?=\n;)/s', $lines, $a);
echo $a[0];

?>

 

But it didn't really work..

 


 

I think it may help If i explain the entire situation..

 

After pulling the file onto my server, I need to cut-out a chunk between two lines !CLIENTS: and ;

After its cut-out I need to seperate each line into its own variable, and then explode those variables into an array breaking it up at every :

After I have a lot of arrays, I will need to loop through them all and insert the arrays into a database

 

NOTE: all the lines have the same number of values, sometimes they are blank,sometimes they have content.. either way I need to put them into the database

Link to comment
Share on other sites

sasa - I don't believe this situation warrants the use of regex... The syntax is fairly tight, and regex is extremely slow compared to PHP's basic string functions.

 

discomatt, that works but it seems to dump everything into a single array? how would I run run a string explode on the array? (is it possible)

 

I assume it would look something like this..

 

//The code you gave me is above
$clients = explode(":", $filtered[1]);

//Then I take that array, insert into a database on a loop

 

EDIT:

Also I just realized, that by doing the stropos, it elimiates the line breaks that used to be there..

 

No, the line breaks will still be there. My example was based on what you asked for. Please provide me with a more detailed explanation, or several examples of sample input data and how you would like the data to be returned.

 

Here is an example line:

 

WDA126:145975:Lorenzo Aiello KSAN:PILOT::52.19151:-106.75558:12904:248:T/DHC7/F:220:CYYC:13000:CKE2:USA-S3:100:1:2200:::2:I:1530:1530:1:40:4:30:CYXE:V/CHARTS/CALLSIGN - WARDAIR 126:YC R10 XE DCT CK32:51.113889:-114.020278:::::20080811151928:94:29.75:1007.31:

 

Please note that that is one entire line..

Link to comment
Share on other sites

channge

$lines = file( $file );

to

$lines = file_get_contents( $file );

 

Thanks, but that still outputs just the content.. the next thing would be to break each line into its own variable (ie $user1, $user2, etc... )

 

problem is that the number of lines change often... maybe a foreach?

Link to comment
Share on other sites

OK

next step explode $lines with "\n"(new line)

 

Ok, so now I have each line in a large array..

 

Now, I need to separate each line into its own array being split on : and then have each different value for each array input into the database

 

each line is equivalent to a MySQL row.. each value is equivalent to a column/datacell in that row

 

(would now be a good time to start a loop? i will need a loop to insert everything into a database anyways.,..)

Link to comment
Share on other sites

I think it may help If i explain the entire situation..

 

After pulling the file onto my server, I need to cut-out a chunk between two lines !CLIENTS: and ;

After its cut-out I need to seperate each line into its own variable, and then explode those variables into an array breaking it up at every :

After I have a lot of arrays, I will need to loop through them all and insert the arrays into a database

 

NOTE: all the lines have the same number of values, sometimes they are blank,sometimes they have content.. either way I need to put them into the database

 

THIS is what I wanted ;) Okay, is there only going to be a single set of lines you want to extract? Or will the file possibly look like this

 

!SOMETHING:
some:values:here
!STATS:
!SOMETHING:
some:other:values
!STATS:
!SOMETHING:
some:more:values
!STATS:

Link to comment
Share on other sites

<?php

$file = 'contents.txt';
$start = '!SOMETHING:';
$end = '!STATS:';

# Put file to an array
$lines = file( $file );

# Set up placeholders
$loop = TRUE;
$capture = FALSE;
$filtered = array();

# Loop through lines, until we've found $end
for( $i = 0, $count = count($lines); $i < $count && $loop === TRUE; $i++ ) {

# Check to see if capturing has been turned on
if ( $capture === TRUE ) {
	# Check to see if this is the endline
	if ( strpos($lines[$i], $end) === 0 )
		# End the loop
		$loop = FALSE;
	else
		# Explode and add to filtered results
		$filtered[] = explode( ':', trim($lines[$i] ) );
}
# Check to see if this is the starting line
elseif ( strpos($lines[$i], $start) === 0 )
	# Turn on capturing
	$capture = TRUE;

} 

# Output results, use foreach loop(s) to build your query
print_r( $filtered );

?>

Link to comment
Share on other sites

I think it may help If i explain the entire situation..

 

After pulling the file onto my server, I need to cut-out a chunk between two lines !CLIENTS: and ;

After its cut-out I need to seperate each line into its own variable, and then explode those variables into an array breaking it up at every :

After I have a lot of arrays, I will need to loop through them all and insert the arrays into a database

 

NOTE: all the lines have the same number of values, sometimes they are blank,sometimes they have content.. either way I need to put them into the database

 

THIS is what I wanted ;) Okay, is there only going to be a single set of lines you want to extract? Or will the file possibly look like this

 

No, its continous, like this:

 

!SOMETHING:

some:values:here

some:values:here

some:values:here

some:values:here

!STATS:

 

!SOMETHING:
some:values:here
!STATS:
!SOMETHING:
some:other:values
!STATS:
!SOMETHING:
some:more:values
!STATS:

Link to comment
Share on other sites

Database structure:

 

CREATE TABLE `clients` (

  `id` int(200) NOT NULL auto_increment,

  `callsign` varchar(255) NOT NULL,

  `cid` varchar(255) NOT NULL,

  `realname` varchar(255) NOT NULL,

  `clienttype` varchar(255) NOT NULL,

  `frequency` varchar(255) NOT NULL,

  `latitude` varchar(255) NOT NULL,

  `longitude` varchar(255) NOT NULL,

  `altitude` varchar(255) NOT NULL,

  `groundspeed` varchar(255) NOT NULL,

  `planned_aircraft` varchar(255) NOT NULL,

  `planned_tascruise` varchar(255) NOT NULL,

  `planned_depairport` varchar(255) NOT NULL,

  `planned_altitude` varchar(255) NOT NULL,

  `planned_destairport` varchar(255) NOT NULL,

  `server` varchar(255) NOT NULL,

  `protrevision` varchar(255) NOT NULL,

  `rating` varchar(255) NOT NULL,

  `transponder` varchar(255) NOT NULL,

  `facilitytype` varchar(255) NOT NULL,

  `visualrange` varchar(255) NOT NULL,

  `planned_revision` varchar(255) NOT NULL,

  `planned_flighttype` varchar(255) NOT NULL,

  `planned_deptime` varchar(255) NOT NULL,

  `planned_actdeptime` varchar(255) NOT NULL,

  `planned_hrsenroute` varchar(255) NOT NULL,

  `planned_minenroute` varchar(255) NOT NULL,

  `planned_hrsfuel` varchar(255) NOT NULL,

  `planned_minfuel` varchar(255) NOT NULL,

  `planned_altairport` varchar(255) NOT NULL,

  `planned_remarks` varchar(255) NOT NULL,

  `planned_route` varchar(255) NOT NULL,

  `planned_depairport_lat` varchar(255) NOT NULL,

  `planned_depairport_lon` varchar(255) NOT NULL,

  `planned_destairport_lat` varchar(255) NOT NULL,

  `planned_destairport_lon` varchar(255) NOT NULL,

  `atis_message` varchar(255) NOT NULL,

  `time_last_atis_recieved` varchar(255) NOT NULL,

  `time_logon` varchar(255) NOT NULL,

  `heading` varchar(255) NOT NULL,

  `QNH_iHg` varchar(255) NOT NULL,

  `QNH_Mb` varchar(255) NOT NULL,

  PRIMARY KEY  (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

 

Its a lot i know.. if you could just show a code that inserts one of the lines, and i can do the rest..

Link to comment
Share on other sites

Holy crap :D

 

Well, the easiest way to do it is like this

 

foreach( $filtered as $filter ) {

echo 'Column 1: ' . $filter[0] . '<br>';
echo 'Column 2: ' . $filter[1] . '<br>';
echo 'Column 3: ' . $filter[2] . '<br>';

}

 

But instead of echo'ing, you'd assign to a query string or something....

 

If you give me some REAL data and a REAL finished database query, I could probably give you a more detailed example.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.