monkeypaw201 Posted August 11, 2008 Share Posted August 11, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/ Share on other sites More sharing options...
corbin Posted August 11, 2008 Share Posted August 11, 2008 http://php.net/strpos Simply check if a line starts with !SOMETHING (strpos(blah, bleh) will be === 0 if it starts with it), and then gather content until a line starts with !STATS. Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613264 Share on other sites More sharing options...
monkeypaw201 Posted August 11, 2008 Author Share Posted August 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613268 Share on other sites More sharing options...
monkeypaw201 Posted August 11, 2008 Author Share Posted August 11, 2008 bump Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613543 Share on other sites More sharing options...
trq Posted August 11, 2008 Share Posted August 11, 2008 Do you need to use php? sed is much better designed for this type of thing. Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613545 Share on other sites More sharing options...
monkeypaw201 Posted August 11, 2008 Author Share Posted August 11, 2008 Thorpe, I don't need to use PHP, but its the most familiar language and I need to take the results and put it through into a database afterwards... Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613547 Share on other sites More sharing options...
discomatt Posted August 11, 2008 Share Posted August 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613621 Share on other sites More sharing options...
monkeypaw201 Posted August 11, 2008 Author Share Posted August 11, 2008 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.. Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613650 Share on other sites More sharing options...
sasa Posted August 11, 2008 Share Posted August 11, 2008 try <?php $test = ';comments ; !STATS: A:B:C !SOMETHING: A:B:C !STATS: A:B:C'; $out = preg_match('/(?<=!SOMETHING:\n).*?(?=\n!STATS:)/s', $test, $a); echo $a[0]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613659 Share on other sites More sharing options...
discomatt Posted August 11, 2008 Share Posted August 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613664 Share on other sites More sharing options...
monkeypaw201 Posted August 11, 2008 Author Share Posted August 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613665 Share on other sites More sharing options...
monkeypaw201 Posted August 11, 2008 Author Share Posted August 11, 2008 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.. Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613668 Share on other sites More sharing options...
sasa Posted August 11, 2008 Share Posted August 11, 2008 channge $lines = file( $file ); to $lines = file_get_contents( $file ); Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613685 Share on other sites More sharing options...
monkeypaw201 Posted August 11, 2008 Author Share Posted August 11, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613691 Share on other sites More sharing options...
sasa Posted August 11, 2008 Share Posted August 11, 2008 OK next step explode $lines with "\n"(new line) Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613694 Share on other sites More sharing options...
monkeypaw201 Posted August 11, 2008 Author Share Posted August 11, 2008 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.,..) Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613700 Share on other sites More sharing options...
discomatt Posted August 11, 2008 Share Posted August 11, 2008 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: Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613705 Share on other sites More sharing options...
discomatt Posted August 11, 2008 Share Posted August 11, 2008 <?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 ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613706 Share on other sites More sharing options...
monkeypaw201 Posted August 11, 2008 Author Share Posted August 11, 2008 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: Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613708 Share on other sites More sharing options...
discomatt Posted August 11, 2008 Share Posted August 11, 2008 My code above will be what you want then. Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613710 Share on other sites More sharing options...
monkeypaw201 Posted August 11, 2008 Author Share Posted August 11, 2008 My code above will be what you want then. I tried it out and it breaks everything up into a massive array... how can I loop (never been good at loops) and every x number of variables (from array) get inserted into MySQL? Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613713 Share on other sites More sharing options...
discomatt Posted August 11, 2008 Share Posted August 11, 2008 Well, the short answer is foreach. The long answer involves me needing to see your database structure Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613722 Share on other sites More sharing options...
monkeypaw201 Posted August 11, 2008 Author Share Posted August 11, 2008 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.. Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613727 Share on other sites More sharing options...
discomatt Posted August 11, 2008 Share Posted August 11, 2008 Holy crap 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. Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613735 Share on other sites More sharing options...
monkeypaw201 Posted August 11, 2008 Author Share Posted August 11, 2008 What do you mean by REAL finished database query? Quote Link to comment https://forums.phpfreaks.com/topic/119105-solved-filtering-content-from-a-file/#findComment-613738 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.