function remove_line($file, $remove) {
$lines = file($file, FILE_IGNORE_NEW_LINES);
foreach($lines as $key => $line) {
if($line === $remove) unset($lines[$key]);
}
$data = implode(PHP_EOL, $lines);
file_put_contents($file, $data);
}
Kaynak: https://stackoverflow.com/questions/5712878/how-to-delete-a-line-from-the-file-with-php
Örnek bir çalışma olması açısından;
test.php olarak kaydedin
$file = fopen("metin.txt","r");
if (filesize("metin.txt")) {
while (!feof($file)) {
$result = fgets($file) . '
';
echo $result;
}
} else echo "Dosya boş";
fclose($file);
function remove_line($file, $remove) {
$lines = file($file, FILE_IGNORE_NEW_LINES);
foreach ($lines as $key => $line) {
if (stripos($line, $remove) !== false) unset($lines[$key]);
}
$data = implode(PHP_EOL, $lines);
file_put_contents($file, $data);
}
if (isset($_GET['sil'])) {
remove_line("metin.txt",$_GET['sil']);
header("Location:test.php");
}
metin.txt olarak kaydedin;
satir1
satir2
satir3
satir4
satir5
satir6
satir7
satir8
satir9
sati***