在我所有文件的开头和结尾添加新行的快速方法是什么


What would be a fast way to adding new lines of code at the beginning and end of all my files?

这可能是一个愚蠢的问题,但有没有一种方法可以将代码行包裹在我的所有PHP脚本中,而不必遍历每个脚本并手动执行?我有200个脚本,想要一个快速的解决方案。

我正在使用Dreamweaver,所以我想也许我可以使用正则表达式来快速完成这项工作,但遗憾的是,我不太擅长正则表达式。

我想用包装我的脚本

<?php
require_once dirname(FILE) . '/../header.php';
?>

<?php
require_once dirname(FILE) . '/../footer.php';
?>

php脚本和以下代码可以编写所需的代码,以开始和结束文件夹中的每个文件。

$code_at_start = "<?php
require_once dirname(FILE) . '/../header.php';
?>";
$code_at_end ="<?php
require_once dirname(FILE) . '/../footer.php';
?>";
foreach (glob("path/to/files/*") as $filename)
{
    $file = file_get_contents($filename);
    file_put_contents($filename, $code_at_start . $file . $code_at_end ));
}

小谷歌会救你的。您可以使用自动追加和自动预置来完成您需要的操作。在.htaccess文件中:

准备文件

php_value auto_prepend_file"/dir/path/header.php"

将文件附加到页面底部

php_value auto_append_file"/dir/path/templates/footer.php"

一篇好文章会向你解释这一点。http://davidwalsh.name/prepend-append-files-htaccess