How to delete a line from the file with php?
$contents = file_get_contents($dir);
$contents = str_replace($line, '', $contents);
file_put_contents($dir, $contents);
PHP How to delete a specific line in a text file using user input?
Use this:
$row_number = 0; // Number of the line we are deleting
$file_out = file("file.txt"); // Read the whole file into an array
//Delete the recorded line
unset($file_out[$row_number]);
//Recorded in a file
file_put_contents("file.txt", implode("", $file_out));
how to delete a single line in a txt file with php
You can use str_replace
$content = file_get_contents('database-email.txt');
$content = str_replace('igor@gmx.de,', '', $content);
file_put_contents('database-email.txt', $content);
how to delete a specific line from a file starting a string in php
Try with this:
<?php
$f = "data.dat";
$term = "this";
$arr = file($f);
foreach ($arr as $key=> $line) {
//removing the line
if(stristr($line,$term)!== false){unset($arr[$key]);break;}
}
//reindexing array
$arr = array_values($arr);
//writing to file
file_put_contents($f, implode($arr));
?>
How to Remove Some Line from Text File Using PHP?
Use the file($path)
function to get the lines into an array, then loop through it.
$lines = file($path, FILE_IGNORE_NEW_LINES);
$remove = "balblalbllablab";
foreach($lines as $key => $line)
if(stristr($line, $remove)) unset($lines[$key]);
$data = implode('\n', array_values($lines));
$file = fopen($path);
fwrite($file, $data);
fclose($file);
How to delete first 11 lines in a file using PHP?
Every example here won't work for large/huge files. People don't care about the memory nowadays. You, as a great programmer, want your code to be efficient with low memory footprint.
Instead parse file line by line:
function saveStrippedCsvFile($inputFile, $outputFile, $lineCountToRemove)
{
$inputHandle = fopen($inputFile, 'r');
$outputHandle = fopen($outputFile, 'w');
// make sure you handle errors as well
// files may be unreadable, unwritable etc…
$counter = 0;
while (!feof($inputHandle)) {
if ($counter < $lineCountToRemove) {
fgets($inputHandle);
++$counter;
continue;
}
fwrite($outputHandle, fgets($inputHandle) . PHP_EOL);
}
fclose($inputHandle);
fclose($outputHandle);
}
How can I remove the last line of a file using php?
This should works :
<?php
// load the data and delete the line from the array
$lines = file('filename.txt');
$last = sizeof($lines) - 1 ;
unset($lines[$last]);
// write the new data to the file
$fp = fopen('filename.txt', 'w');
fwrite($fp, implode('', $lines));
fclose($fp);
?>
Related Topics
How to Extend a Class Using More Than 1 Class in PHP
Get Table Column Names in MySQL
How to Return Outer HTML of Domdocument
How to Stop People Hacking the PHP-Based Highscore Table of a Flash Game
How to Use Shell_Exec Without Waiting For the Command to Complete
Http Authentication Logout Via PHP
Convert Backslash-Delimited String into an Associative Array
How to Load a Controller from Another Controller in Codeigniter
Pdo Parameterized Query - Reuse Named Placeholders
MySQLi Prepared Statements Error Reporting
Example of How to Use Bind_Result VS Get_Result
"Invalid Parameter Number: Parameter Was Not Defined" Inserting Data
How to Fix Error: Laravel.Log Could Not Be Opened
How to Pass Data to All Views in Laravel 5