如何动态添加正文标签 ih php.


How to add body tags dynamically ih php

我在服务器上有很多文件,我需要通过php脚本进行修改。我想在文件内容之前和之后添加 html 标签。

你的问题不清楚。基本上,您可以这样做:

$file_name = 'something';
$old_content = file_get_contents($file_name);
$html_before = '<html>... something before the old content';
$html_after = '</html>... something after the old content';
$result = $html_before . $old_content . $html_after;
file_put_contents($file_name, $result); // overwrite the original file. make sure you have backup for that.

如果要对目录中的所有文件执行此操作,可以尝试:

$dir_path = '/path/to/your/dir';
$dir = dir($dir_path);
if ($dir !== false) {
    while (($item = $dir->read()) !== false) {
        if ($item == '.' || $item == '..') continue; // skip . and ..
        $path = $dir->path . '/' . $item;
        if (is_dir($path)) continue; // skip directory
        $old_content = file_get_contents($path);
        $html_before = '<html>... something before the old content';
        $html_after = '</html>... something after the old content';
        $result = $html_before . $old_content . $html_after;
        file_put_contents($path, $result); // overwrite the original file!
    }
    $dir->close();
}