如何用php替换特定的行。我不知道行号。我想替换包含特定单词的行。如何用php替换文本文件中的特定行?
回答
一种方法,你可以在更小的文件使用一种可以放入你的内存的两倍:
$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');
}
如果您不知道该行,则必须搜索所有行。
iterate over the file line by line或read the file into memory all at once。 然后或者找到组合为strpos
和str_replace
或 使用preg_replace
的单词。
如果迭代,只需使用strpos
,并且在未返回FALSE时替换该行。然后将文件保存回磁盘。
您必须覆盖整个文件。
因此,对于文件相对较小的文件read file into array,搜索该词,替换找到的行,将all the rest写入file。
对于大文件,算法略有不同,但通常情况相同。
重要组成部分,是file locking
这就是为什么我们喜欢的数据库。
对'flock()'的注释+1 – gnarf 2010-06-09 08:32:22
$filedata = file('filename');
$newdata = array();
$lookfor = 'replaceme';
$newtext = 'withme';
foreach ($filedata as $filerow) {
if (strstr($filerow, $lookfor) !== false)
$filerow = $newtext;
$newdata[] = $filerow;
}
现在包含该文件的内容作为数组(使用implode()
如果你不想数组)中含“replaceme”与“回事,直接”替换行。
您也可以使用多行模式使用正则表达式当然
preg_match_all('/word}/m', $textfile, $matches);
这是,假设它是在准备和装载较小的文档。否则,其他答案远比解决方案的“现实世界”要多。
也许这可以帮助:
$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'])) ;
}
这是好事,如果你正在寻找一个线串(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,砖层
- 1. 如何替换文本文件中的特定行
- 2. 如何将文本替换为文本文件中的特定行?
- 3. 如何在bash脚本中替换文件的特定行?
- 4. 如何替换文本文件中特定的lat和long值?
- 5. UNIX替换文件中的特定行
- 6. 从特定行中替换文本
- 7. 用文本块替换特定文本
- 8. 替换文本文件中的特定行
- 9. 如何使用Java替换文件中的特定行?
- 10. 如何替换包含特定值的文本文件中的一行(批次)
- 11. 替换两个文件的特定行
- 12. jQuery替换href中的特定文本
- 13. 用php替换段落中的特定文本模式
- 14. 从文本文件中替换JSON中的特定字段
- 15. Python替换文本文件中的行
- 16. 替换python文本文件中的行
- 17. 如何使用Asp.net替换文件名(特定的特定文件夹)?
- 18. 使用单行shell命令替换所有文件中的特定文本
- 19. 替换文本文件中特定点的数字
- 20. VB.Net替换大文本文件中的特定值
- 21. 从文本文件中替换特定的字符串
- 22. 替换文件中的特定文本C
- 23. 替换PHP中的文本?
- 24. 替换服务器上所有php文件中的特定位置文本?
- 25. 如何用Perl替换文件中的特定IP?
- 26. 如何用新文本替换仅SWT中的选定文本
- 27. 如何执行替换文件中的文本并将替换后的文本粘贴到一个文件中的特定位置
- 28. 如何使用bat文件脚本替换文本文件中的文本?
- 29. 如何使用.SH文件脚本替换文本文件中的文本?
- 30. 替换非标准文本文件的特定行中的字符串
Thanq .....它工作正常 – kishore 2010-06-09 10:38:08
更少的内存密集型方法正是我所需要的(简单易懂).....谢谢! – scottcarmich 2015-02-06 21:54:39