如何用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. 使用php替换文本文件中的特定行?
- 2. PHP替换文件中的特定行
- 3. 如何替换文本文件中的特定行
- 4. 使用python替换文本文件中的特定行?
- 5. 如何将文本替换为文本文件中的特定行?
- 6. 如何在bash脚本中替换文件的特定行?
- 7. 替换文本文件中特定行中的字符串
- 8. 如何替换文本文件中特定的lat和long值?
- 9. UNIX替换文件中的特定行
- 10. 从特定行中替换文本
- 11. 用文本块替换特定文本
- 12. 如何用php替换文件的行
- 13. 替换文本文件中的特定行
- 14. 如何替换PHP中的特定文本
- 15. 如何使用Java替换文件中的特定行?
- 16. 如何替换c中某一行中的特定文本#
- 17. 如何替换包含特定值的文本文件中的一行(批次)
- 18. 查找文件中的特定文本并替换该文件
- 19. 替换两个文件的特定行
- 20. 用特定字符串替换vi文本文件中的备用行
- 21. jQuery替换href中的特定文本
- 22. 替换文本中的特定链接
- 23. 用php替换段落中的特定文本模式
- 24. 从文本文件中替换JSON中的特定字段
- 25. 用文本文件中的格式化输出替换特定的行
- 26. 如何使用Asp.net替换文件名(特定的特定文件夹)?
- 27. 替换python文本文件中的行
- 28. 替换文本文件中的一行
- 29. Python替换文本文件中的行
- 30. 使用单行shell命令替换所有文件中的特定文本
Thanq .....它工作正常 – kishore 2010-06-09 10:38:08
更少的内存密集型方法正是我所需要的(简单易懂).....谢谢! – scottcarmich 2015-02-06 21:54:39