2013-07-03 45 views
0

我需要根據單個CSV文件的內容在單個目錄中重新編寫多個文件。搜索並替換整個目錄文件內容

例如,CSV文件將包含類似這樣:

define("LANG_BLABLA", "NEW"); 

在該目錄將包含這個文件之一:

define("LANG_BLABLA", "OLD"); 

該腳本將通過目錄搜索和任何發生在CSV「LANG_BLABLA」與舊目錄LANG相匹配的情況下,它將用「NEW」更新「OLD」

我的問題是,在1個數組中的目錄中的文件的nts,所以我可以輕鬆地搜索它們並在必要時進行替換。

謝謝。

+0

你確定它是一個CSV文件,而不是'define's的PHP文件嗎? – Ollie

+0

你不「列出」文件的內容。你想知道如何列出目錄中的文件(遞歸?),或者如何根據文件的完整路徑讀取/替換/保存文件? – Jon

+0

我無法想象爲什麼這將是一個很好的解決方案,你的問題,你的eaxmple不會是有效的csv,也不知道如何才能知道什麼來取代什麼? – Anigel

回答

0

陣列通過目錄搜索是比較容易的:

<? 
clearstatcache(); 
$folder = "C:/web/website.com/some/folder"; 
$objects = scandir($folder, SCANDIR_SORT_NONE); 
foreach ($objects as $obj) { 
    if ($obj === '.' || $obj === '..') 
     continue; // current and parent dirs 
    $path = "{$folder}/{$obj}"; 
    if (strcasecmp(substr($path, -4), '.php') !== 0) 
     continue // Not a PHP file 
    if (is_link($path)) 
     $path = realpath($path); 
    if (! is_file($path)) 
     continue; // Not a file, probably a folder 
    $data = file_get_contents($path); 
    if ($data === false) 
     die('Some error occured...') 
    // ... 
    // Do your magic here 
    // ... 
    if (file_put_contents($path, $data) === false) 
     die('Failed to write file...'); 
} 

至於修改PHP文件動態,它可能是你需要一個標誌將這些東西放到數據庫或內存中的數據存儲中...... MySQL,SQLite,MongoDB,memcached,Redis等應該這樣做。你應該使用哪一個取決於你的項目的性質。

-1

您可以分析一個CSV文件導入使用fgetcsvhttp://php.net/manual/en/function.fgetcsv.php

+0

謹慎解釋倒票? – Ollie

+0

解析CSV不是問題,問題在於解析目錄中所有文件的內容,以便腳本可以查找並替換 –

+0

中存在的任何LANG_如果將所有文件解析爲不同的數組,則您可以比較它們。 – Ollie

0

首先,如果您使用.php文件,我不會推薦此工作流程。嘗試集中您的定義語句,然後在一個位置進行更改。

但是,這是一個解決方案,應該爲你的csv文件工作。它不完整,你必須添加一些你想要的邏輯。

/** 
* Will return an array with key value coding of your csv 
* @param $defineFile Your file which contains multiple definitions e.g. define("LANG_BLABLA", "NEW");\n define("LANG_ROFL", "LOL"); 
* @return array 
*/ 
public function getKeyValueArray($defineFile) 
{ 
    if (!file_exists($defineFile)) { 
     return array(); 
    } else { 
     $fp = @fopen($defineFile, 'r'); 
     $values = explode("\n", fread($fp, filesize($defineFile))); 
     $newValues = array(); 
     foreach ($values as $val) { 
      preg_match("%.*\"(.*)?\",\s+\"(.*)?\".*%", $val, $matches); 
      $newValues[$matches[1]] = $matches[2]; 
     } 
    } 
} 
/** 
* This is s stub! You should implement the rest yourself. 
*/ 
public function updateThings() 
    { 
     //Read your definition into an array 
     $defs=$this->getKeyValueArray("/some/path/to/your/file"); 
     $scanDir="/your/desired/path/with/input/files/"; 
     $otherFiles= scandir($scanDir); 
     foreach($otherFiles as $file){ 
      if($file!="." && $file!=".."){ 
       //read in the file definition 
       $oldDefinitionArray=$this->getKeyValueArray($scanDir.$file); 
       //Now you have your old file in an array e.g. array("LANG_BLABLA" => "OLD") 
       //and you already have your new file in $defs 
       //You now loop over both and check for each key in $defs 
       //if its value equals the value in the $oldDefinitionArray. 
       //You then update your csv or rewrite or do whatever you like. 
      } 
     } 
    }