我打算使用php创建一个简单的代码,可以替换文件中的字符串,就像在记事本和ms字中替换函数一样,但区别在于它会将所有匹配的字符串替换为所需的字符串,以便在所有文件中更改文件夹任何想法怎么做?
答案 0 :(得分:0)
您可以使用有用的glob() php函数。
示例强>
<?php
$files_in_your_folder = glob('c:\wamp\www\FindReplace\*');
foreach(glob('c:\wamp\www\FindReplace\*') as $path_to_file) {
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace("Hello","World",$file_contents);
file_put_contents($path_to_file,$file_contents);
}
?>
如果目录中有子文件夹,则可以使用以下代码
获取文件$path = realpath(__DIR__ . '/textfiles/'); // Path to your textfiles
$files_in_your_folder = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::SELF_FIRST);