简体   繁体   中英

Have over 360 .php files, forgot to include footer.php in all of them. What's the fastest way to do this?

I am working on a project. I'm almost close to finished. I am using CodeIgniter as a framework and I have over 360 main files in my views folder. However, I only have included <?php include "includes/footer.php" ?> in about 3 functions in my controller. I have over 360 functions and 200 controllers.

What's the fastest way to include a footer file in every one of the pages?

I'd use something such as Notepad++ . With regular expression matching, you can easily add that to the bottom of files:

Here are the settings I used to accomplish this.

The fastest (and most easily configurable) way to insert '' into alot of files (on linux command line) is sed:

find views/ -name '*.php' -exec sed -i.bak '$ i\<?php include "includes/footer.php" ?>' {} +

Explanation:

  1. -i.bak means "in-place" edit and create backup copy with '.bak' suffix. Any string append to -i causes backup file creation, and the string is appended to the backup filename. A bare -i causes only "in-place" editing and no backup file creation. Remove the -i disables in-place editing and causes output to std out.
  2. $ means "match last line". You could replace $ with /PATTERN/ , where PATTERN is a regex.
  3. i\\ means insert

The effect of this command is the literal string after the backslash, up to the last single quote, will be inserted before the last line of each file found by find.

It's a bit of a problem but you can maybe search and replace multiple files... eg if you are loading views and have your footer after the body then replace your previous view ie $this->load->view('home_view', $data); with the same string but then add your footer as well?

Notepad++ as suggested would be a good idea or:

Find and replace in multiple files

You could easily write a simple PHP script that: opens each file in the directory, appends the needed code to that file, and closes the file. You would then rinse and repeat for every file.

EDIT: Needed to provide more details for answer.

Some pseudocode below. Note its pythonic in style (since thats what I am familiar with but the general gist is the same).

def appendCode(file):
    code = "<?php require(\"footer.php\"); ?>"
    with open(file, 'a') as f:
        #append code here to end of file.
        f.append(f)

def readDir(dir):
    if isDir(dir):
        d = openDir(dir)

    return d.allFiles()

def main():
    dirs = ["dir1", "dir2"]

    for dir in dirs:
        files = readDir(dir)

        for f in files:
            appendCode(f)

Alternatively, you could use a one liner command prompt that have been posted here or linked to! I personally enjoy writing simple programs like this just for the fun of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM