查找并替换文件 [英] Find and replace in a file

查看:79
本文介绍了查找并替换文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本文件中将某些字符串替换为另一字符串(例如:\nH,H).使用PHP有什么办法吗?

I want to replace certain strings with another one in a text file (ex: \nH with ,H). Is there any way to that using PHP?

推荐答案

您可以使用

You could read the entire file in with file_get_contents(), perform a str_replace(), and output it back with file_put_contents().

示例代码:

<?php

$path_to_file = 'path/to/the/file';
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace("\nH",",H",$file_contents);
file_put_contents($path_to_file,$file_contents);

?>

这篇关于查找并替换文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆