查找并替换文件
问题描述:
我想在文本文件中将某些字符串替换为另一字符串(例如:\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?
答
您可以使用 str_replace(),然后使用
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);
?>