2015-08-16 49 views
2

我有一個文件大約2000行,我正在處理。這是一個簡單的閱讀,取代文字和回寫,然而,領先的空白不被保存。php讀寫文件不保存領先的空白

任何想法?

<?php 
    $access = fopen("oldfile.txt", "r"); 
    $y=9999; 
    for ($i=1; $i<=$y; $i++) 
     { 
     $line = trim(fgets($access)); 
     $loc = strpos($line, "findtext", 0); 
     if ($loc) 
      { 
      $loc = $loc +6; 
      $end = strpos($line, "endtext", 0); 
      $pull = substr($line, $loc, ($end-$loc)); 

      $loc = strpos($line, 'foundthis', 0); 
      $end = $loc +12; 

      $newline = substr($line, 0, $loc).'foundthis'.$pull.'" '. 
    substr($line, $end); 

      } 
     else 
      { $newline = $line; } 

     file_put_contents("newfile.txt", $newline."\r\n", FILE_APPEND); 

     } 
    fclose($access); 

?> 

oldfile.txt 
    labelinput "Adhesives" name="MyName"Adhesiveslabel 
    labelinput "Cord, Yarn & Material" name="MyName"Cord, Yarn & 
     labelinput "Corners" name="MyName"Cornerslabel 
      labelinput "Ink & Ink Pads" name="MyName"Ink & Ink Padslabel 

newfile.txt 
labelinput "Adhesives" name="MyName"Adhesiveslabel 
labelinput "Cord, Yarn & Material" name="MyName"Cord, Yarn & Materiallabel 
labelinput "Corners" name="MyName"Cornerslabel 
labelinput "Ink & Ink Pads" name="MyName"Ink & Ink Padslabel 

回答

2

如果你想空白,因爲它是修改下面一行

$line = trim(fgets($access)); 

$line = fgets($access); 

這應該解決您的問題。

+0

這就是我來這裏的原因。深夜蟲子。謝謝您的幫助。 – user3314053

+0

@ user3314053希望它解決了您的問題。享受編碼:-) – Butterfly

+0

@ user3314053如果它解決接受它http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Butterfly