2011-04-19 31 views
8

我有一個文件名爲$dir和字符串命名$line,我知道這個字符串是該文件的完整產品線,但我不知道它的行號,我想從文件中刪除,我該怎麼辦?如何刪除從PHP文件中的一行?

是否有可能用awk?

+0

AWK是一個外部程序,你需要使用EXEC或類似功能的 – Raisen 2011-04-19 08:21:54

回答

7
$contents = file_get_contents($dir); 
$contents = str_replace($line, '', $contents); 
file_put_contents($dir, $contents); 
+8

沒有工作的朋友 – ibrahim 2011-04-19 07:36:24

+0

我試過每行之間寫回聲的代碼,它的罰款,直到file_put_contents功能,我不明白爲什麼它不工作:S – ibrahim 2011-04-19 08:38:38

+0

@ibrahim - 你什麼意思沒有工作?你能通過'file_get_contents'獲得所有內容嗎?告訴我哪一部分沒有工作。 – 2011-04-19 08:41:42

17

閱讀線一個接一個,並寫入所有,但匹配的行到另一個文件。然後替換原始文件。

+0

怎麼樣awk來稱呼它?有可能在PHP中使用? – ibrahim 2011-04-19 07:25:04

+1

爲什麼要麻煩。 PHP可以自行處理它。 – 2011-04-19 07:28:06

+0

在這個文件中可能有成千上萬行,所以我想用最有效的方式來做:) – ibrahim 2011-04-19 07:38:49

3

另一種方法是逐行讀取文件中的行,直到找到一個匹配,則截斷該文件到這一點,然後追加行的其餘部分。

+1

你如何得到這些行?你剛纔截斷了這個文件。 – 2011-04-19 07:23:34

+0

... ionno,將它們放到內存中,然後截斷.long,因爲文件不是太大。不是說這是一個更好的解決方案..雖然應該導致更少的磁盤寫入。 – mpen 2011-04-19 21:05:19

7

這將只是看在每一行,如果它不是你想刪除的內容,它就會被推到,將獲得回寫到文件的數組。 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); 
+1

太棒了!這工作正常 – 2015-02-24 22:12:52

0

像這樣:

file_put_contents($filename, str_replace($line . "\r\n", "", file_get_contents($filename))); 
1

這也是好的,如果你正在尋找一個線串(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,磚層

0

我想處理文件的最好方法是把它們編輯爲字符串。

首先,獲取文件中的所有行(以下代碼可以被壓縮):

$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()無法找到它)。

0

它可以在不使用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); 
} 
+0

SO正在詢問awk。你的回答可能會解決問題,但試着解釋你如何解決問題。 – Michael 2017-12-10 05:41:46