2015-10-14 126 views
0

我想修改我使用PHP的文本文件或者我也可以使用C#,我在一個文本文件的工作文件包含字符串例如如何修改一個文件的PHP

TM_len = --------------------------------------------
EMM_len - ------------------------------------------
T_len = 45 CTGCCTGAGCTCGTCCCCTGGATGTCCGGGTCTCCCCAGGCGG
NM_ = 2493 ---------------- ATATAAAAAGATCTGTCTGGGGCCGAA

我想從文件中刪除這四行,如果我發現一行僅包含「-」字符,並且當然保存到文件。

回答

1

也許這樣?我寫它在一個容易理解,「不,縮短」的方式:

$newfiledata = ""; 
$signature = " "; 
$handle = fopen("inputfile.txt", "r"); // open file 
if ($handle) { 
    while (($line = fgets($handle)) !== false) { // read line by line 
     $pos = strpos($line, $signature); // locate spaces in line text 
     if ($pos) { 
      $lastpart = trim(substr($line, $pos)); // get second part of text 
      $newstring = trim(str_replace('-', '', $line)); // remove all dashes 

      if (len($newstring) > 0) $newfiledata .= $line."\r\n"; // if still there is characters, append it to our variable 
     } 
    } 

    fclose($handle); 
} 
// write new file 
file_put_contents("newfile.txt", $newfiledata);