PHP操作txt文档,修改数据 .

simon803 2008-04-05 02:44:32
比如有个文本文档a.txt,内容是
1.1.1.1
2.2.2.2
3.3.3.3
4.4.4.4
5.5.5.5

如何修改a.txt的每条数据,修改后为b.txt:
10.10.10.10
20.20.20.20
30.30.30.30
40.40.40.40
50.50.50.50
即每个段*10 .
...全文
588 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hookee 2008-04-05
  • 打赏
  • 举报
回复

<?php
$content = file_get_contents('C:\Inetpub\wwwroot\a.txt');
$arr = explode("\r\n", $content);
$s = "";
for($i=0;$i<count($arr);$i++){
$items = explode(".", $arr[$i]);
for($j=0;$j<count($items);$j++){
$s .= $items[$j]*10;
if($j != count($items)-1) $s .= '.';
}
$s .= "\r\n";
}
file_put_contents('C:\Inetpub\wwwroot\b.txt',$s);
?>
Unending 2008-04-05
  • 打赏
  • 举报
回复
<?php
$ha = fopen("a.txt","r");
$hb = fopen("b.txt","w");
while ($nn = fscanf($ha, "%d.%d.%d.%d")) {
list ($n1, $n2, $n3, $n4) = $nn;
fprintf($hb,"%d.%d.%d.%d\r\n",$n1*10,$n2*10,$n4*10,$n4*10);
}
fclose($ha);
fclose($hb);
?>
tantaiyizu 2008-04-05
  • 打赏
  • 举报
回复
读取后 拼接后 重新写入
Unending 2008-04-05
  • 打赏
  • 举报
回复
这种是基础问题,不是什么疑难杂症
建议楼主朋友当遇到基础问题不能解决那就多读教程,而不是到论坛求助
即便有人代劳了,问题稍加变化楼主恐怕还是要再度求助

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧