網站有很多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",
這樣就可以了,非常感謝