2017-02-18 50 views
0

我需要刪除部分以</div></div></div>開頭的文本,以及在臨時目錄中的每個文件中的所有文本。 文本stucture基本相同:刪除每個文件中的部分文本

<!DOCTYPE html> 
<head> 

blah blah lots of text.... 

</div></div></div> 

blah blah lots of text.... 

</div> 

</html> 

我做了什麼:

$files = scandir(__DIR__ . "/temp/"); 
$files = array_diff($files, array('.', '..')); 

foreach($files as $file) { 

    $text_file = file_get_contents(__DIR__ . "/temp/".$file); 

    foreach($text_file as $txfile) { 

     $txfile = strstr($txfile, '</div></div></div>', true); 
     echo $txfile; 
    } 
} 

Warning: Invalid argument supplied for foreach() 

,並試圖通過這種方式

$files = scandir(__DIR__ . "/temp/"); 
$files = array_diff($files, array('.', '..')); 

foreach($files as $file) { 

    $text_file = file_get_contents(__DIR__ . "/temp/".$file); 

    for($i=0;$i<count($text_file);$i++) 
    { 

     $text_file = strstr($text_file[$i], '</div></div></div>', true); 
     echo $text_file; 
    } 
} 

甚至試圖與glob但仍然沒有結果strstr文件。

回答

1

$text_file = file_get_contents(__DIR__ . "/temp/".$file);$text_file將是一個string不是array,所以使用foreach將拋出一個錯誤。 您可以嘗試

foreach($files as $file) { 

    $text_file = file_get_contents(__DIR__ . "/temp/".$file); 

     $txfile = strstr($text_file, '</div></div></div>', true); 
     echo $txfile; 
}