批量向html中插入內容

來源:互聯網
上載者:User
網站有很多html頁面,現在需要做移動適配,需要在這些html的頁面中插入一行JS引用,
插入這樣的


請教下能不能用php批量修改插入,因為這些頁面結構都是一樣的,還是只能手動一個個的修改


回複討論(解決方案)

foreach('路徑/*.hyml') as $fn) {  file_put_contents($fn, str_replace('', '', file_get_contents($fn)));}

批量轉換工具.exe

謝謝,我去試試

foreach('路徑/*.hyml') as $fn) {  file_put_contents($fn, str_replace('', '', file_get_contents($fn)));}



用這個方法會提示:Invalid argument supplied for foreach(),我是這麼寫
foreach(('tiezi/20140610/*.html') as $fn) {  file_put_contents($fn, str_replace('', '', file_get_contents($fn)));}

/** 遍曆檔案夾及檔案類*   Date:   2013-03-21*   Author: fdipzone*   Ver:    1.0**   Funcitons*   process 遍曆檔案及檔案夾*   handle  處理檔案方法*/class FindFile{ // class start    public $files = array();    // 儲存遍曆的檔案    protected $maxdepth;        // 搜尋深度,0表示沒有限制    /*  遍曆檔案及檔案夾    *   @param String $spath     檔案夾路徑    *   @param int    $maxdepth  搜尋深度,預設搜尋全部    */    public function process($spath, $maxdepth=0){        if(isset($maxdepth) && is_numeric($maxdepth) && $maxdepth>0){            $this->maxdepth = $maxdepth;        }else{            $this->maxdepth = 0;        }        $this->files = array();        $this->traversing($spath); // 遍曆    }    /*  遍曆檔案及檔案夾    *   @param String $spath 檔案夾路徑    *   @param int    $depth 當前檔案夾深度    */    private function traversing($spath, $depth=1){        if($handle = opendir($spath)){            while(($file=readdir($handle))!==false){                if($file!='.' && $file!='..'){                    $curfile = $spath.'/'.$file;                    if(is_dir($curfile)){ // dir                        if($this->maxdepth==0 || $depth<$this->maxdepth){ // 判斷深度                            $this->traversing($curfile, $depth+1);                        }                    }else{  // file                        $this->handle($curfile);                    }                }            }            closedir($handle);        }    }    /** 處理檔案方法    *  @param String $file 檔案路徑    */    protected function handle($file){        if(strtolower(substr($file,-4))=='html'){            file_put_contents($file, str_replace('', '', file_get_contents($file)));        }    }} // class end$obj = new FindFile();$obj->process('tiezi/20140610');

不好意思,我漏了關鍵函數

foreach(glob('tiezi/20140610/*.html') as $fn) {  file_put_contents($fn, str_replace('', '', file_get_contents($fn)));}

可以定義一個方法,載入js檔案的,如:

php

$jslink = "";
$jslink .= "";
$jslink .= "";
$jslink .= "";
$jslink .= "";
$this->assign('jslin',$jslink);


html
{$jslink}

很多頁面結構一樣的html?
我個人建議樓主先整理一下架構...

不好意思,我漏了關鍵函數

foreach(glob('tiezi/20140610/*.html') as $fn) {  file_put_contents($fn, str_replace('', '', file_get_contents($fn)));}



這個OK,現在想問下,這種添加出來的格式是:,在源檔案中和JS
引用在一行,請問下怎麼能讓換行,這樣好看點
在網上找了方法,
file_put_contents($fn, str_replace('', '\r\n',
添加'\r\n',沒用

\r\n 在雙引號中才會生效

file_put_contents($fn, str_replace('', "\r\n",

\r\n 在雙引號中才會生效

file_put_contents($fn, str_replace('', "\r\n",


這樣就可以了,非常感謝
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.