2010-06-09 138 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,砖层

相关问题