I want to replace the line matching
I can spot the ISS at XX Hrs.
with blank or remove the line.
I have a text file, the sample content is as follows.
I can spot the ISS at 0800 Hrs.
Random string....
Random string....
Random string....
Random string....
I can spot the ISS at 0900 Hrs.
Random string....
I can spot the ISS at 2000 Hrs.
Random string....
Random string....
Random string....
What I have tried:
The best way to solve the problem will be using REGEX, but as I’m noob on REGEX I’m using combination of 2 string replace.
Replace the line
I can spot the ISS at
Replace the line
0800 Hrs. / 0900 Hrs. / …
The problem with the approach is it’s simply a VERY BAD WAY.
chorn
April 14, 2020, 4:56am
2
sourav17ghosh:
I’m noob on REGEX
you can still try that, show the results and what problems you encounter.
sourav17ghosh:
I want to replace the line matching
I can spot the ISS at XX Hrs.
with blank or remove the line.
I have a text file, the sample content is as follows.
If the lines are in a text file then I would use the following PHP function:
https://www.php.net/manual/en/function.file.php
No Regex but a lot easier to follow and/or modify - for me anyway
<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');
$aLines = file('txt.txt');
$aAfter = fnModify( $aLines );
echo '<pre>';
echo '<b>BEFORE: </b> $aLines ==> ';
print_r($aLines);
echo '<b>AFTER:</b> $aAfter ==> ';
print_r($aAfter);
echo '</pre>';
//==============================================
function fnModify
(
array $aResult = []
)
:array
{
foreach($aResult as $key => $line) :
# NUMERIC POSITIVE IF TRUE OTHERWISE ZERO bool false
if( strpos($line, 'spot the ISS') ) :
$aResult[$key] = 'INSERTED TEXT ==> ' .strtoupper($aResult[$key]);
$line = 'INSERT STUFF HERE ==> ' .$line;
endif;
# echo '<br>' .$key .' ==> ' .$line;
endforeach;
return $aResult;
}
Output
BEFORE: $aLines ==> Array
(
[0] => I can spot the ISS at 0800 Hrs.
[1] => Random string…
[2] => Random string…
[3] => Random string…
[4] => Random string…
[5] => I can spot the ISS at 0900 Hrs.
[6] => Random string…
[7] =>
[8] => I can spot the ISS at 2000 Hrs.
[9] => Random string…
[10] => Random string…
[11] => Random string…
)
AFTER: $aAfter ==> Array
(
[0] => INSERTED TEXT ==> I CAN SPOT THE ISS AT 0800 HRS.
[1] => Random string…
[2] => Random string…
[3] => Random string…
[4] => Random string…
[5] => INSERTED TEXT ==> I CAN SPOT THE ISS AT 0900 HRS.
[6] => Random string…
[7] =>
[8] => INSERTED TEXT ==> I CAN SPOT THE ISS AT 2000 HRS.
[9] => Random string…
[10] => Random string…
[11] => Random string…
)
system
Closed
July 14, 2020, 12:51pm
4
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.