2010-06-09 61 views

回答

39

一種方法,你可以在更小的文件使用一種可以放入你的內存的兩倍:

$data = file('myfile'); // reads an array of lines 
function replace_a_line($data) { 
    if (stristr($data, 'certain word')) { 
    return "replaement line!\n"; 
    } 
    return $data; 
} 
$data = array_map('replace_a_line',$data); 
file_put_contents('myfile', implode('', $data)); 

快速注意,PHP> 5.3.0支持lambda函數,因此您可以刪除指定的函數聲明,縮短地圖上:

$data = array_map(function($data) { 
    return stristr($data,'certain word') ? "replacement line\n" : $data; 
}, $data); 

理論上你就可以使這個單一的(難以追隨)PHP語句:

file_put_contents('myfile', implode('', 
    array_map(function($data) { 
    return stristr($data,'certain word') ? "replacement line\n" : $data; 
    }, file('myfile')) 
)); 

另一個(更少的內存密集型)的方法,你應該對大文件使用:

$reading = fopen('myfile', 'r'); 
$writing = fopen('myfile.tmp', 'w'); 

$replaced = false; 

while (!feof($reading)) { 
    $line = fgets($reading); 
    if (stristr($line,'certain word')) { 
    $line = "replacement line!\n"; 
    $replaced = true; 
    } 
    fputs($writing, $line); 
} 
fclose($reading); fclose($writing); 
// might as well not overwrite the file if we didn't replace anything 
if ($replaced) 
{ 
    rename('myfile.tmp', 'myfile'); 
} else { 
    unlink('myfile.tmp'); 
} 
+0

Thanq .....它工作正常 – kishore 2010-06-09 10:38:08

+0

更少的內存密集型方法正是我所需要的(簡單易懂).....謝謝! – scottcarmich 2015-02-06 21:54:39

5

您必須覆蓋整個文件。

因此,對於文件相對較小的文件read file into array,搜索該詞,替換找到的行,將all the rest寫入file

對於大文件,算法略有不同,但通常情況相同。

重要組成部分,是file locking

這就是爲什麼我們喜歡的數據庫。

+0

對'flock()'的註釋+1 – gnarf 2010-06-09 08:32:22

2
$filedata = file('filename'); 
$newdata = array(); 
$lookfor = 'replaceme'; 
$newtext = 'withme'; 

foreach ($filedata as $filerow) { 
    if (strstr($filerow, $lookfor) !== false) 
    $filerow = $newtext; 
    $newdata[] = $filerow; 
} 

現在​​包含該文件的內容作爲數組(使用implode()如果你不想數組)中含「replaceme」與「回事,直接」替換行。

3

您也可以使用多行模式使用正則表達式當然

preg_match_all('/word}/m', $textfile, $matches); 

這是,假設它是在準備和裝載較小的文檔。否則,其他答案遠比解決方案的「現實世界」要多。

0

也許這可以幫助:

$data = file("data.php"); 

for($i = 0;$i<count($data);$i++){ 
    echo "<form action='index.php' method='post'>"; 
    echo "<input type='text' value='$data[$i]' name='id[]'><br>"; 
} 

echo "<input type='submit' value='simpan'>"; 
echo "</form>"; 

if(isset($_POST['id'])){ 
    file_put_contents('data.php',implode("\n",$_POST['id'])) ; 
} 
1

這是好事,如果你正在尋找一個線串(ID),並希望更換與新線舊線。

代碼:

$id = "123"; 
$new_line = "123,Programmer\r"; // We're not changing the ID, so ID 123 remains. 
$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 
      $new_contents .= $new_line; // change record to new record 
     }else{ 
      $new_contents .= $record . "\r"; 
     } 
    } 
    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!"); 
} 

實施例:

舊文件:

ID,職業

123,學生

124,磚層

運行代碼將改變文件:

新文件:

ID,職業

123,程序員

124,磚層