共計 555 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 PHP 中,可以使用 file_get_contents()
和file_put_contents()
函數來實現批量替換文件內容。以下是一個簡單的示例代碼:
// 要替換的內容
$search = 'old content';
$replace = 'new content';
// 遍歷文件夾中的所有文件
$files = glob('path/to/files/*');
foreach($files as $file){// 讀取文件內容
$content = file_get_contents($file);
// 替換內容
$newContent = str_replace($search, $replace, $content);
// 將替換后的內容寫回文件
file_put_contents($file, $newContent);
}
在上面的示例中,首先定義了要替換的內容 $search
和$replace
,然后使用 glob()
函數獲取指定文件夾中的所有文件,遍歷每個文件并使用 file_get_contents()
讀取文件內容,再使用 str_replace()
函數將舊內容替換為新內容,最后使用 file_put_contents()
函數將替換后的內容寫回文件中。
丸趣 TV 網 – 提供最優質的資源集合!
正文完