0

I have a Delete link in my profile page which links to "delete.php?snum=" . $snum. I have a $snum=$_GET['snum']; in delete.php. I have a file named "SLBrecords.csv" which looks like

12345 Barney
67890 Stinson

and I would like to create a script that would search for $snum in the file and delete the line that contains it. I think this is easy but I keep messing with implode, strpos, and other functions now but I'm still not able to make it work.

EDIT: Here is my code.

<?php

$snum=$_GET['snum'];

$filePath = './SLBrecords.csv';

$fileArr = file( $filePath );

foreach($fileArr as $line)
    if (strpos($line,$snum) !== false) {
        unset($line);
    }
$success = FALSE;
if ( file_put_contents( $filePath, implode( '', $fileArr ), LOCK_EX ) )
{
    $success = TRUE;
}

?>

Edit: How can I delete the line and also delete the newline that comes with it?

4

1 回答 1

1

unset($line) is wrong. Try

foreach($fileArr as $k=>$line) {
    if (strpos($line,$snum) !== false) {
        unset($fileArr[$k]);
    }
}
于 2013-04-16T07:11:33.740 回答