回答
$contents = file_get_contents($dir);
$contents = str_replace($line, '', $contents);
file_put_contents($dir, $contents);
另一種方法是逐行讀取文件中的行,直到找到一個匹配,則截斷該文件到這一點,然後追加行的其餘部分。
你如何得到這些行?你剛纔截斷了這個文件。 – 2011-04-19 07:23:34
... ionno,將它們放到內存中,然後截斷.long,因爲文件不是太大。不是說這是一個更好的解決方案..雖然應該導致更少的磁盤寫入。 – mpen 2011-04-19 21:05:19
這將只是看在每一行,如果它不是你想刪除的內容,它就會被推到,將獲得回寫到文件的數組。 see this
$DELETE = "the_line_you_want_to_delete";
$data = file("./foo.txt");
$out = array();
foreach($data as $line) {
if(trim($line) != $DELETE) {
$out[] = $line;
}
}
$fp = fopen("./foo.txt", "w+");
flock($fp, LOCK_EX);
foreach($out as $line) {
fwrite($fp, $line);
}
flock($fp, LOCK_UN);
fclose($fp);
太棒了!這工作正常 – 2015-02-24 22:12:52
像這樣:
file_put_contents($filename, str_replace($line . "\r\n", "", file_get_contents($filename)));
這也是好的,如果你正在尋找一個線串(ID),並希望用一個新行來代替舊線。
代碼:
$contents = file_get_contents($dir);
$new_contents= "";
if(strpos($contents, $id) !== false) { // if file contains ID
$contents_array = preg_split("/\\r\\n|\\r|\\n/", $contents);
foreach ($contents_array as &$record) { // for each line
if (strpos($record, $id) !== false) { // if we have found the correct line
pass; // we've found the line to delete - so don't add it to the new contents.
}else{
$new_contents .= $record . "\r"; // not the correct line, so we keep it
}
}
file_put_contents($dir, $new_contents); // save the records to the file
echo json_encode("Successfully updated record!");
}
else{
echo json_encode("failed - user ID ". $id ." doesn't exist!");
}
實施例:
輸入: 「123,學生」
舊文件:
ID,職業
123,學生
124,磚層
運行代碼將改變文件:
新文件:
ID,職業
124,磚層
我想處理文件的最好方法是把它們編輯爲字符串。
首先,獲取文件中的所有行(以下代碼可以被壓縮):
$file = @fopen($dir, 'r'); # As you said, $dir is your filename
if ($file) { # Ending bracket is at the end
if (filesize($dir)) { # Checks whether the file size is not zero (we know file exists)
$fileContent = fread($file, filesize($dir)); # Reads all of the file
fclose($file);
} else {
// File is empty
exit; # Stops the execution (also you can throw an exception)
}
$fileLineByLine = explode(PHP_EOL, $fileContent); # Divides the file line by line
在這裏,你可以執行搜索:
$key = false; # By default, your string $line is not in the file (false)
foreach ($fileLineByLine as $lineNumber => $thisLine)
if ($thisLine === $line)
$key = $lineNumber; # If $line found in file, set $key to this line number
簡單地說,你可以刪除線$鍵+ 1:
if ($key !== false) # If string $line found in the file
unset($fileLineByLine[$key]); # Remove line $key + 1 (e.g. $key = 2, line 3)
最後,您必須將更改保存到文件:
$newFileContent = implode(PHP_EOL, $fileLineByLine); # Joins the lines together
$file = fopen($dir, "w"); # Clears the file
if ($file) {
fwrite($file, $newFileContent); # Saves new content
fclose($file);
}
} # Ends 'if ($file) {' above
您也可以將上面的代碼設置爲函數。
注:
$線不得有新行字符,如\ n。你必須將其刪除:
$line = str_replace(PHP_EOL, '', $line);
不要使用
$fileLineByLine[$key] = "";
代替
unset($fileLineByLine[$key]);
,因爲第一種情況下不會刪除線,它只是清除線(並且不需要的空行將保留)。在這種情況下,implode()爲$ fileLineByLine [$ key]添加一個新行,該行爲空;否則,如果您取消設置變量,它將不可用(並且implode()無法找到它)。
它可以在不使用AWK來解決:
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);
}
SO正在詢問awk。你的回答可能會解決問題,但試着解釋你如何解決問題。 – Michael 2017-12-10 05:41:46
- 1. 從大文件中刪除一行php
- 2. 如何從文本文件中刪除第一行php
- 3. 如何使用PHP從文本文件中刪除一些行?
- 4. 如何刪除文件中的行(PHP)
- 5. 如何使用php從ini文件中刪除一行?
- 6. 從php文件中刪除特殊行
- 7. 如何從VB6的文本文件中刪除一行文本?
- 8. PHP從文件中刪除
- 9. 從文本文件中刪除一行?
- 10. 從文本文件中刪除一行
- 11. 如何使用php從文本文件中刪除一行而不留空行
- 12. 如何在php中修改或刪除一行文本文件?
- 13. 從PHP中的CSV文件中刪除最上面一行。
- 14. 如何從Python中的文件中刪除一行
- 15. 如何從jQuery/javaScript中的txt文件中刪除一行
- 16. 如何從Perl的大文件中刪除非唯一的行?
- 17. 如何使用PHP從CSV文件中刪除前100行
- 18. Qt從文件中刪除一行
- 19. 從txt文件中刪除一行java
- 20. 從文件中刪除第一行
- 21. 從文件中刪除行
- 22. 從文件中刪除行
- 23. 從文件中刪除行
- 24. 如何從Java文本文件中刪除一行
- 25. python-如何從文本文件中刪除一行?
- 26. 如何從文本文件中刪除一行數據
- 27. 如何從txt文件中刪除一行文本
- 28. 從另一個文件中的文本文件中刪除行
- 29. 如何從php文件夾中刪除文件
- 30. 如何使用PHP從文件夾中刪除文件
AWK是一個外部程序,你需要使用EXEC或類似功能的 – Raisen 2011-04-19 08:21:54