How to remove a line of text from a file based on a variable search in php?
<p>Pardon my mess..I should be able to do this but I am having trouble for some reason.  In the first part, I split the array at the colon and compared variable name to form field name, if it is not already there, I write it to the file.  That works.  In the second part, I need the user to type in two form fields (firstdelete &amp; lastdelete).  If both contain a match to the first name and last name in the text file, I need to delete that line of text from the file.  For some reason, I'm confusing myself and that's where it becomes a holy mess.  Thanks anyone in advance for the hel</p>

<pre><code>//My code:

&lt;form style="margin-left: 80px;" METHOD="POST" action="updating.php"&gt;

&lt;h4&gt;Add a row:&lt;/h4&gt;

First Name: &lt;input type="text" name="firstadd"&gt;&lt;br&gt;
Last Name: &lt;input type="text"  name="lastadd"&gt;&lt;br&gt;&lt;br&gt;

&lt;h4&gt;Delete a row:&lt;/h4&gt; 

First Name: &lt;input type="text" name="firstdelete"&gt;&lt;br&gt;
Last Name: &lt;input type="text"  name="lastdelete"&gt;&lt;br&gt;&lt;br&gt;
&lt;INPUT TYPE="submit" VALUE="Update"&gt;
&lt;/FORM&gt;

&lt;pre&gt;
&lt;?php

//If there is something in these fields, assign it to variable name, else set it to null and assign variable name.
if (isset($_POST["firstadd"]))  {
    $firstadd = $_POST["firstadd"];
}
else {
    $firstadd = ""; 
}

if (isset($_POST["lastadd"]))  {
    $lastadd = $_POST["lastadd"];
}
else {
    $lastadd = ""; 
}

if (isset($_POST["firstdelete"]))  {
    $firstdelete = $_POST["firstdelete"];
}
else {
    $firstdelete = ""; 
}

if (isset($_POST["lastdelete"]))  {
    $lastdelete = $_POST["lastdelete"];
}
else {
    $lastdelete = ""; 
}

$filename = "gradeBook.txt";
if (!file_exists($filename)) {
    print "No such file or directory";
    exit();
}

$fh=fopen($filename, "r+"); //Open the file for reading
while(!feof($fh)){
    $line_of_text=fgets($fh); //Get text line from the file

    //Explode array at the colon.
    $parts[] =  explode(':', $line_of_text, 6);

    //Assign a variable name to each array element in the inner array.
    foreach ($parts as $inner_array){
            $first = @$inner_array[0];
            $last = @$inner_array[1];
            $hw1 = @$inner_array[2];
            $hw2 = @$inner_array[3];
            $test1 = @$inner_array[4];
            $test2 = @$inner_array[5];

            }

         }
            //If firstname and lastname not in the file, write it to the end of the file and print it out in the table.
            $fh = fopen($filename, 'a');
            if($firstadd !== $first &amp;&amp; $lastadd !== $last){
            fwrite($fh,"\n".$firstadd.":".$lastadd);  

            echo"&lt;table border='1' style='margin-left: 150px;'&gt;";
            echo "&lt;tr bgcolor='6666cc'&gt;";
            echo"&lt;td&gt;First Name&lt;/td&gt;&lt;td&gt;Last Name&lt;/td&gt;&lt;td&gt;Hw1&lt;/td&gt;&lt;td&gt;Hw2&lt;/td&gt;&lt;td&gt;Test1&lt;/td&gt;&lt;td&gt;Test2&lt;/td&gt;";
            echo "&lt;tr bgcolor='ff99ff'&gt;";
            echo "&lt;td&gt;" .$firstadd."&lt;/td&gt;". "&lt;td&gt;".$lastadd."&lt;/td&gt;". "&lt;td&gt;".$hw1."&lt;/td&gt;". "&lt;td&gt;".$hw2."&lt;/td&gt;". "&lt;td&gt;".$test1."&lt;/td&gt;". "&lt;td&gt;".$test2."&lt;/td&gt;";
            echo "&lt;/table&gt;";
    }
    fclose($fh);

    //Delete a row if first name and last name match.  Here's where I confused myself.

    $filename = "gradeBook.txt";
    if (!file_exists($filename)) {
    print "No such file or directory";
    exit();
}
            //$backTogether = array();
            $fh = fopen($filename, 'r+');
                while(!feof($fh)){
                    $line_of_text=fgets($fh); //Get text line from the file
                    $lines[]=$line_of_text; //Assign each line to array


                            if($firstdelete == $first &amp;&amp; $lastdelete == $last){
                                    //$backTogether[] = implode(":", $inner_array);
            //$data = array_slice(file('gradeBook.txt'), 0, -5, true));
                            }

            }
            print_r ($lines);

//$outstring = implode(":", $output);
file_put_contents("gradeBook.txt", $outstring);

            echo"&lt;table border='1' style='margin-left: 150px;'&gt;";
            echo "&lt;tr bgcolor='6666cc'&gt;";
            echo"&lt;td&gt;First Name&lt;/td&gt;&lt;td&gt;Last Name&lt;/td&gt;&lt;td&gt;Hw1&lt;/td&gt;&lt;td&gt;Hw2&lt;/td&gt;&lt;td&gt;Test1&lt;/td&gt;&lt;td&gt;Test2&lt;/td&gt;";
            echo "&lt;tr bgcolor='ff99ff'&gt;";
            echo "&lt;td&gt;" .$first."&lt;/td&gt;". "&lt;td&gt;".$last."&lt;/td&gt;". "&lt;td&gt;".$hw1."&lt;/td&gt;". "&lt;td&gt;".$hw2."&lt;/td&gt;". "&lt;td&gt;".$test1."&lt;/td&gt;". "&lt;td&gt;".$test2."&lt;/td&gt;";
            echo "&lt;/table&gt;";

    //} 

fclose($fh); //Close the file

echo '&lt;br&gt;&lt;a href="' . $_SERVER['PHP_SELF'] . '"&gt;Reset page&lt;/a&gt;';

?&gt;
&lt;/pre&gt;

//My text file:

Michelle:James:99:100:88:95
Heather:Jones:48:77:99:88
Brittani:Adams:44:76:66:34
Michael:Scott:99:99:99:88
Michael:Bolton:98:99:79:87
Angus:Young:100:100:25:66
</code></pre>
