3

So My idea is to find a certain line in a text file using a regex then replacing the line with a blank essentially deleting it. However, im struggling with over writing the line with the blank in the text file

 elseif ($inquiry=='delete'){

$file= fopen("database.txt", "r+") or die("File was not found on server"); 

$search = "/^[" . $Title . "%" . $Author . "%" . $ISBN . "%" . $Publisher . "%" . $Year . "]/i";

             //search function
             // What to look for


             // open and Read from file
             $lines = file('database.txt');//array

             foreach($lines as $line){


                 // Check if the line contains the string we're looking for, and print if it does
                 if(preg_match($search, $line)){
                    echo preg_replace($line," ",$search);
                     echo "                          
                     entry deleted-<br>";
                   }
                   else{
                       echo "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                       entry not found<br>";
                   }
             }
             fclose($file);
         }
jman
  • 33
  • 4

1 Answers1

6

This problem can be more simply broken down into two distinct problems.

  1. Find the offset of a substring in a given string
  2. Overwrite the substring from the end of its offset with the remainder of the string

You can accomplish the first with a simple strpos() needle/haystack search. There is no need for a regular expression here.

The second case merely requires knowing the offset and length of the sub string within the string (the file) such that you can take the remainder of the string (the rest of the file) and overwrite from the given offset, truncating the remainder of the file.

There are two distinct approaches to this solution.

  1. You can do the entire operation in memory
  2. You can do it on disk

The first obviously require more memory if the file is large (at least twice the size of the file). The second approach is more conservative on memory, but requires a little more work to implement.

In Memory Implementation

I'll use the in memory implementation as its easier to write and explain.

For the purposes of demonstration let's assume that the file database.txt contains the following:

Line 1
Line 2
Line 3

Let's say we want to remove Line 2 from this file.

$searchString = "Line 2\n"; // The line we want to remove

$string = file_get_contents("database.txt");
$offset = strpos($string, $searchString);

// The part of the file before the search string
$part1 = substr($string, 0, $offset);

// The part of the file after the search string
$part2 = substr($string, $offset + strlen($searchString));

// Now we glue them back together
file_put_contents("database.txt", $part1 . $part2);

You've effectively just deleted the line in question. The file should now look like this...

Line 1
Line 3
user3783243
  • 5,368
  • 5
  • 22
  • 41
Sherif
  • 11,786
  • 3
  • 32
  • 57
  • what would my search string be if im grabbing text from input fields in a form? – jman Apr 05 '20 at 04:25
  • The search string is whatever text you're searching for. The same text you were putting in your regular expression but without the regular expression. – Sherif Apr 05 '20 at 04:27