drug95107 2013-03-07 09:59
浏览 60
已采纳

如何使用PHP更新静态HTML文件

We've 700 static HTML files in folder "music". We want to put analytic code in the end of the HTML files.. like before

</body> </html>

tags.

Please anybody let me know, how its possible with PHP code?

  • 写回答

1条回答 默认 最新

  • drsdvwsvo78320812 2013-03-07 10:05
    关注

    Too easy. Find all files, read content, modify content, save content.

    <?php
    
    // open directory
    if($handle = opendir(__DIR__)) {
        // search for
        $search = '</body>';
    
        // replace with (</body> gets appended later in the script)
        $replace = <<< EOF
    
    <!-- your analytics code here -->
    
    EOF;
    
        // loop through entries
        while(false !== ($entry = readdir($handle))) {
            if(is_dir($entry)) continue; // ignore entry if it's an directory
    
            $content = file_get_contents($entry); // open file
            $content = str_replace($search, $replace . '</body>', $content); // modify contents
            file_put_contents($entry, $content); // save file
        }
    }
    
    echo 'done';
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于php中URL传递GET全局变量的问题
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件