Replacing string with partial match

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.

  1. Replace the line

I can spot the ISS at

  1. Replace the line

0800 Hrs. / 0900 Hrs. / …

The problem with the approach is it’s simply a VERY BAD WAY.

you can still try that, show the results and what problems you encounter.

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 :slight_smile:

<?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

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.