Remove a block of text from a text file

Hi All

I play microsoft flight simulator and I would like to print out my flight plans.
The flight plans are saved as i.e. birmingham_to_France_Charles.FLT

Within the flight plan there are many repeated instructions

is there away of deleting the block of text starting from:

[ATC_ActiveFlightPlan.0]
To: waypoint.5=, TNCM, , TNCM, A, N18? 2.51’, W63? 6.79’, +000007.21, , , , , , NONE, 0, 0, -1, 0, 0,

[ATC_ActiveFlightPlan.0]
title=Birmingham to Princess Juliana Intl
description=Birmingham to Princess Juliana Intl
type=VFR
routetype=0
cruising_altitude=36500
departure_id=EGBB, N52? 27.23', W1? 44.88', +000315.50
departure_position=PARKING 23 PARKING NONE
destination_id=TNCM, N18? 2.51', W63? 6.79', +000007.21
departure_name=Birmingham
destination_name=Princess Juliana Intl
waypoint.0=, EGBB, , EGBB, A, N52? 27.23', W1? 44.88', +000315.50, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.1=, , , TIMECLIMB, U, N52? 25.32', W1? 42.75', +000915.50, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.2=, , , TIMECRUIS, U, N42? 45.71', W33? 37.27', +036500.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.3=, , , TIMEDSCNT, U, N35? 20.99', W45? 15.71', +036500.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.4=, , , TIMEAPPROACH, U, N18? 0.89', W63? 17.52', +002050.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.5=, TNCM, , TNCM, A, N18? 2.51', W63? 6.79', +000007.21, , , , , , NONE, 0, 0, -1, 0, 0,

I can open the file and remove stuff by using
$text = str_replace(‘AirlineCallSign’, ‘Call Sign’, $text);
and then saving the file back to the server.
Cheers

Cleggie

1 Like

Yes. You can. It is call Regular Expression, however you have to put more data from the file the regex can be tested properly (no just the block of data you want removed)

1 Like

Will these strings for the start and end always be the same?

Just based on what you have given us, this seems to do the trick

Edited based on post#5

\[ATC_ActiveFlightPlan\.0\][\s\S]+?waypoint\.5.*$

Breakdown:

\[ATC_ActiveFlightPlan\.0\] - match [ATC_ActiveFlightPlan.0] (need to escape special characters e.g. [ or.)

[\s\S]+? - match non-space and space characters. Similar to . which is any character, but will match new lines as well. Simply put this will match any character over multiple lines. The ? makes it non greedy.

waypoint\.5.*$ - The non-greedy will match the previous bit of regex up to this. Which is waypoint.5 followed by . any character, * 0 or many times up to the end of the line $

Hi

Here is the first half of the text file, anything after this I can remove by using str_replace.

[Assistance]
EventTriggersFileCount=1
EventTriggersFile.0=EventTriggers

[Main]
Title=Birmingham to Princess Juliana Intl
Description=Birmingham to Princess Juliana Intl
AppVersion=10.0.61355
FlightVersion=1
MissionType=FreeFlight
MissionLocation=Birmingham
OriginalFlight=
FlightType=NORMAL
StartingCameraCategory=Custom

[Options]
Sound=True
Moonlight=True
Save=False
SaveOriginalFlightPlan=False
TextDisplayPage=0
SlewDisplayPage=1
AxisIndicator=Off

[Sim.0]
Sim=Airbus A320 Neo Asobo
Pilot=Pilot_Female_Uniform
Copilot=Pilot_Female_Uniform
TailNumber=1234
AirlineCallSign=CLEGG01
FlightNumber=12FG
AppendHeavy=False

[ResourcePath]
Path=Missions\Asobo\FreeFlights\FreeFlight\FreeFlight

[ObjectFile]
File=Missions\Asobo\FreeFlights\FreeFlight\FreeFlight

[Atc_Menu]
Visible=True

[FreeFlight]
FirstFlightState=PREFLIGHT_GATE

[Weather]
UseWeatherFile=False
UseLiveWeather=True
WeatherPresetFile=
WeatherCanBeLive=True
CloudmapPosOverride=False
CloudmapInitialPosX=0
CloudmapInitialPosY=0
FixedClouds=False
DebugPublicAirflowDisplay=False

[Departure]
ICAO=EGBB
GateName=PARKING
GateNumber=23
GateSuffix=NONE

[Arrival]
ICAO=TNCM
RunwayNumber=10
RunwayDesignator=NONE

[Loading]
ImageName0=LOADING_FREEFLIGHT
Tips0=asobo-aircraft-a320-neo,TT:LOADING.TIPS.A320NEO_001,TipCategory_Aircraft
Tips1=asobo-aircraft-a320-neo,TT:LOADING.TIPS.A320NEO_001,TipCategory_Aircraft
Tips2=asobo-aircraft-a320-neo,TT:LOADING.TIPS.A320NEO_002,TipCategory_Aircraft
Tips3=asobo-aircraft-a320-neo,TT:LOADING.TIPS.A320NEO_002,TipCategory_Aircraft

[TrafficOptions]

[Panels]
Panel.On=True
HUD.On=False

[Window.1]
Order=255
Active=True
Undocked=False
Maximized=False
ScreenUniCoords=0, 0, 8192, 2800
UndocCoords=0, 0, 0, 0

[ATC_AgentManager]
NumberofAgents=0

[ATC_Aircraft.0]
ActiveFlightPlan=True
RequestedFlightPlan=True
AcState=ACSTATE_REQUEST_TAXI_CLEARANCE_OUT_VFR_ATIS
Waypoint.0=, EGBB, , EGBB, A, N52� 27.23', W1� 44.88', +000315.50, , , , , , NONE, 0, 0, -1, 0, 0,  
Waypoint.1=, , , TIMECLIMB, U, N52� 25.32', W1� 42.75', +000915.50, , , , , , NONE, 0, 0, -1, 0, 0,  
Waypoint.2=, , , TIMECRUIS, U, N42� 45.71', W33� 37.27', +036500.00, , , , , , NONE, 0, 0, -1, 0, 0,  
Waypoint.3=, , , TIMEDSCNT, U, N35� 20.99', W45� 15.71', +036500.00, , , , , , NONE, 0, 0, -1, 0, 0,  
Waypoint.4=, , , TIMEAPPROACH, U, N18� 0.89', W63� 17.52', +002050.00, , , , , , NONE, 0, 0, -1, 0, 0,  
Waypoint.5=, TNCM, , TNCM, A, N18� 2.51', W63� 6.79', +000007.21, , , , , , NONE, 0, 0, -1, 0, 0,  
NumberofWaypoints=6
ClearanceFlags=882
CtCur=CLEARANCE_NONE
WaypointNext=2
AltCleared=-1
HdgAssigned=-1
SquawkAssigned=4608
LandingSequence=LANDING_NONE
DepartureRequest=DEPARTURE_VFR_REQUEST_NONE
ParkingRequest=PARKING_NAME_NONE
ParkingTypeRequest=PARKING_TYPE_NONE
PatternLeg=PATTERN_LEG_NONE
ApproachIndex=-1
ApproachTransitionIndex=-1
ApproachRequestIndex=-1
ApproachTransitionRequestIndex=-1
RunwayIndex=-1
RunwayRequestIndex=-1
ParkingIndex=-1
FlightFollowing=FLIGHT_FOLLOWING_NO
AirspaceTransition=AIRSPACE_TRANSITION_NONE
BVAirspaceTransition=BV_TYPE_NONE
BVAirspaceTransitionReq=BV_TYPE_NONE
TakeoffSequence=TAKEOFF_SEQUENCE_NONE
LandingSequenceNumber=-1
TaxiRouteCurrent=-1
CruisingAltitude=36500
RequestedCruisingAltitude=-1
ExpectedAltitude=-1
AircraftSignature=1,0

[ATC_ActiveFlightPlan.0]
title=Birmingham to Princess Juliana Intl
description=Birmingham to Princess Juliana Intl
type=VFR
routetype=0
cruising_altitude=36500
departure_id=EGBB, N52� 27.23', W1� 44.88', +000315.50
departure_position=PARKING 23 PARKING NONE
destination_id=TNCM, N18� 2.51', W63� 6.79', +000007.21
departure_name=Birmingham
destination_name=Princess Juliana Intl
waypoint.0=, EGBB, , EGBB, A, N52� 27.23', W1� 44.88', +000315.50, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.1=, , , TIMECLIMB, U, N52� 25.32', W1� 42.75', +000915.50, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.2=, , , TIMECRUIS, U, N42� 45.71', W33� 37.27', +036500.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.3=, , , TIMEDSCNT, U, N35� 20.99', W45� 15.71', +036500.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.4=, , , TIMEAPPROACH, U, N18� 0.89', W63� 17.52', +002050.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.5=, TNCM, , TNCM, A, N18� 2.51', W63� 6.79', +000007.21, , , , , , NONE, 0, 0, -1, 0, 0,  

[ATC_RequestedFlightPlan.0]
title=Birmingham to Princess Juliana Intl
description=Birmingham to Princess Juliana Intl
type=IFR
routetype=3
cruising_altitude=38000
departure_id=EGBB, N52� 27.23', W1� 44.88', +000315.50
departure_position=PARKING 23 PARKING NONE
destination_id=TNCM, N18� 2.51', W63� 6.79', +000007.21
departure_name=Birmingham
destination_name=Princess Juliana Intl
waypoint.0=, EGBB, , EGBB, A, N52� 27.23', W1� 44.88', +000315.50, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.1=EG, 2IBIR, EGBB, 2IBIR, I, N52� 25.13', W1� 42.60', +000800.00, , LUXT15, , , 15, NONE, 0, 0, -1, 0, 0,  
waypoint.2=EG, 4IBIR, EGBB, 4IBIR, I, N52� 23.15', W1� 41.66', +001200.00, , LUXT15, , , 15, NONE, 0, 0, -1, 0, 0,  
waypoint.3=EG, LUXTO, , LUXTO, I, N52� 8.51', W2� 3.64', +005150.00, , LUXT15, , , 15, NONE, 0, 0, -1, 6000, 0, A
waypoint.4=EI, LUVIS, , LUVIS, I, N51� 23.40', W9� 28.52', +038000.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.5=EG, H4916, , H4916, I, N49� 30.00', W16� 0.00', +038000.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.6=EG, H4722, , H4722, I, N47� 30.00', W22� 0.00', +038000.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.7=EG, H4528, , H4528, I, N45� 30.00', W28� 0.00', +038000.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.8=LP, H4333, , H4333, I, N43� 30.00', W33� 0.00', +038000.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.9=, , , TIMECRUIS, U, N42� 50.86', W33� 55.15', +038000.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.10=LP, H4037, , H4037, I, N40� 30.00', W37� 0.00', +038000.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.11=KZ, H3841, , H3841, I, N38� 30.00', W41� 0.00', +038000.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.12=KZ, 3645N, , 3645N, I, N36� 0.00', W45� 0.00', +038000.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.13=, , , TIMEDSCNT, U, N35� 25.03', W45� 43.44', +038000.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.14=KZ, H3348, , H3348, I, N33� 30.00', W48� 0.00', +038000.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.15=KZ, H3052, , H3052, I, N30� 30.00', W52� 0.00', +038000.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.16=KZ, H2755, , H2755, I, N27� 30.00', W55� 0.00', +038000.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.17=KZ, H2458, , H2458, I, N24� 30.00', W58� 0.00', +038000.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.18=KZ, H2161, , H2161, I, N21� 30.00', W61� 0.00', +038000.00, , , , , , NONE, 0, 0, -1, 0, 0,  
waypoint.19=, 0008, , AFirst, U, N18� 14.61', W63� 23.30', +005000.00, , , ULUBA1, , 10, NONE, 0, 0, -1, 0, 0,  
waypoint.20=TN, ULUBA, TNCM, ULUBA, I, N18� 0.89', W63� 17.49', +002050.00, , , ULUBA1, , 10, NONE, 0, 0, -1, 0, 0,  
waypoint.21=, TNCM, , TNCM, A, N18� 2.51', W63� 6.79', +000000.00, , , , , , NONE, 0, 0, -1, 0, 0,  

[ATC_AircraftManager]
NumberofAircraft=1

[ATC_MessageSystem]
FrequencyNodes=0

As you can see the away points are repeated a few times within a block of text, so, I would like to remove the unwanted blocks of text.
When I create a new flight plan the information within the blocks can be more or less information.

many thanks for your fast reply

Cheers
Cleggie

Are you specifically after the block starting [ATC_ActiveFlightPlan.0] or do you want to match other blocks? e.g. starting with [ATC_RequestedFlightPlan.0] etc

If you only want the [ATC_ActiveFlightPlan.0] block, then post#4 seems to work.

Hi All

Here is my code:

<?php

$file = file (“uploads/Manchester to Helsinki.FLT”);

$pattern = “[ATC_ActiveFlightPlan.0][\s\S]+?waypoint.5.*$”;
$replacement = “-”;

// Replace
echo preg_replace($pattern, $replacement, $file);

echo “

”;
print_r($file);
echo “
”;

// Now save the new file to a new file name

$newfile = “downloads/tempnew.txt”;

if(!is_file($newfile)) {
file_put_contents($newfile, $file); // Save our content to the file.

}
//

?>

however, the content of the block is still there, which part of the code have i got incorrect. :thinking:

Cheers
Cleggie

Don’t you need to capture the return from preg_replace() to get the modified string, or does it modify it in place? It seems (though it’s difficult to read as you didn’t put code tags around it) as if you’re just echoing it. Maybe your line should read

$file = preg_replace($pattern, $replacement, $file);

(sorry for the vagueness, I haven’t done anything with regular expressions).

Yes, that’s part of the problem, the modified string is just echoed out, it’s never assigned to a variable to do anything with it.

The other issue is the pattern.

There is no escaping of regex characters.

$pattern = "\[ATC_ActiveFlightPlan\.0\][\s\S]+?waypoint\.5.*$";

Just want to point out I had escaped those characters in post#4 :slight_smile:

Hi All

I can remove Individual words or a line of text, but, I still cannot remove the block of text.

$file = file ("uploads/Manchester to Helsinki.FLT");

// $pattern = "\[ATC_ActiveFlightPlan\.0\][\s\S]+?waypoint\.5.*$";
$pattern ="[ATC_ActiveFlightPlan.0]";
$replacement = "-";

// Replace
// $file = str_replace($pattern, $replacement, $file);

 $file = preg_replace($pattern, $replacement, $file);

echo "<pre>";
print_r($file);
echo "</pre>";

As you can see I can use either str_replace or preg_replace is there away to remove the block of text by starting at [ATC_ActiveFlightPlan.0] and then remove each line afterwards until I come to a empty line.

Cheers
Cleggie

"\[ATC_ActiveFlightPlan\.0\].*?(\r?\n){2}"
(Note that you’ll want to put one of those line breaks back in, probably)

Hi m_hutley

Cheers for the fast reply, however the code is still there when I print it to the screen, when I use preg_replace with your code all I get is:

Array
(
)

When I use the str_replace I get all the text file printed to the screen.
Is my code the correct code for what I am trying to achieve.

<!DOCTYPE html>
<html lang="en">
<head>

<meta http-equiv="Content-Type" 
      content="text/html; charset=utf-8">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php

$file = file ("uploads/Manchester to Helsinki.FLT");

// $pattern = "\[ATC_ActiveFlightPlan\.0\][\s\S]+?waypoint\.5.*$";
$pattern ="\[ATC_ActiveFlightPlan\.0\].*?(\r?\n){2}";
$replacement = "-";

// Replace
$file = str_replace($pattern, $replacement, $file);

// $file = preg_replace($pattern, $replacement, $file);

echo "<pre>";
print_r($file);
echo "</pre>";

//
?>
</body>
</html>

many thanks
Cleggie

str_replace cant find this pattern. This has to be preg_replace’d.

EDIT: Also you’ll need the appropriate flags on the pattern. Specifically, the s flag must be set in order for the .*? to reach across multiple lines.

Try this:

$pattern = ‘/[ATC_ActiveFlightPlan.0][\s\S]+?waypoint.5.*$/m’;

m stand for multiline

Hi All

Many thanks for all the advise, however, I have tried all the Regular expression’s one by one and still when I display the file the content is still there.

<!DOCTYPE html>
<html lang="en">
<head>

<meta http-equiv="Content-Type" 
      content="text/html; charset=utf-8">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php

$file = file ("uploads/Manchester to Helsinki.FLT");

// $pattern = "\[ATC_ActiveFlightPlan\.0\][\s\S]+?waypoint\.5.*$";
 
// $pattern = "/[ATC_ActiveFlightPlan.0][\s\S]+?waypoint.5.*$/m";

	$pattern = "/\[ATC_ActiveFlightPlan\.0\].*?(\r?\n){2}/s";

 // $pattern ="\[ATC_ActiveFlightPlan\.0\].*?(\r?\n){2}";

$replacement = "-";

// Replace

$file = preg_replace($pattern, $replacement, $file);

echo "<pre>";
print_r($file);
echo "</pre>";

//

?>
</body>
</html>

I can’t seam to figure out where I am am going wrong, I have tried this using Php 7.3 all the way up to Php 8

Once again many thanks
Cleggie

xnFtHi - Online PHP Interpreter & Debugging Tool - Ideone.com

Works for me?

Oh wait, i see where you’re going wrong.

replace this:
$file = file ("uploads/Manchester to Helsinki.FLT");

with this;
$file = file_get_contents("uploads/Manchester to Helsinki.FLT");

that way $file is a string, not an array. The pattern needs to walk across a multiline string.