2012-10-23 66 views
0
$test =array('APP_TITLE' =>'ssfss'); 
update_define($test); 

function update_define($arr_con){ 
    $file='config.php'; 
    $fileContent= file_get_contents($file); 
    $i = 0; 
    $define_arr = array(); 
    $new_value_arr = array(); 

    foreach ($arr_con as $constant => $vale){ 
     $line = getLine($constant); 

     $define_arr[$i] = $line; 
     if(($vale == 'true') || ($vale == 'false')){ 
      $new_value_arr[$i] = "define('$constant', $vale)"; 
     }else{ 
      $new_value_arr[$i] = "define('$constant', '$vale')"; 
     } 
     $i++; 
    } 
    $modified=preg_replace($define_arr, $new_value_arr, $fileContent); 

    file_put_contents($file, $modified); 

} 

function getLine($string){ 
    $ret = ''; 
    $file = fopen("config.php", "r") or exit("Unable to open file!"); 
    //Output a line of the file until the end is reached 
    $i = 0; 
    while(!feof($file)) 
    { 
     $i++; 
     $line = fgets($file); 
     $pos = strpos($line, $string); 
     if($pos !== false){ 
      $test =array('APP_TITLE' =>'ssfss'); 
      update_define($test); 

function update_define($arr_con){ 
    $file='config.php'; 
    $fileContent= file_get_contents($file); 
    $i = 0; 
    $define_arr = array(); 
    $new_value_arr = array(); 

      foreach ($arr_con as $constant => $vale){ 
       $line = getLine($constant); 

       $define_arr[$i] = $line; 
       if(($vale == 'true') || ($vale == 'false')){ 
        $new_value_arr[$i] = "define('$constant', $vale)"; 
       }else{ 
        $new_value_arr[$i] = "define('$constant', '$vale')"; 
       } 
       $i++; 
       } 
       $modified=preg_replace($define_arr, $new_value_arr, $fileContent); 

       file_put_contents($file, $modified); 

      } 

     function getLine($string) 
     { 

     $ret = ''; 
      $file = fopen("config.php", "r") or exit("Unable to open file!"); 
      //Output a line of the file until the end is reached 
      $i = 0; 
      while(!feof($file)) 
      { 
       $i++; 
       $line = fgets($file); 
       $pos = strpos($line, $string); 
       if($pos !== false){ 
        //   echo $i.'<br/>'; 
        $line = str_replace("define(", "define\(",$line); 
        $line = str_replace(");","\);", $line); 
        $ret= $line; 
       } 
      } 
      fclose($file); 
      return $ret; 
     } 
       $line = str_replace("define(", "define\(",$line); 
        $line = str_replace(");","\);", $line); 
        $ret= $line; 
       } 
      } 
      fclose($file); 
      return $ret; 
     } 

config.php文件的preg_replace():分隔符不能是字母,數字或反斜線

<?PHP 
    define ('APP_TITLE', 'test com title'); 
    define('COMPANY_ADDRESS_2', '49 sd d'); 
    define('COMPANY_ZIPCODE', '2085'); 
    define('COMPANY_PHONE', '+44 (0)2 8007-5554'); 
    define('COMPANY_FAX', '+62 (0)2 253-9479'); 
?> 

上面的代碼給我下面的錯誤

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in /opt/lampp/htdocs/Edit_file/abc.php on line 46 
Array ([0] => define\('APP_TITLE', 'test com title' \);) /nArray ([0] => define('APP_TITLE', 'ssfss')) 

我」被特林編輯一些PHP文件使用我的上述方法,但調用preg_replace()方法給我一些錯誤。請幫忙解決這個問題

+0

'$ define_arr = Array([0] => define \('APP_TITLE','some test'\));' 爲什麼你要反斜槓括號? –

+0

而不是'preg_replace',它使用正則表達式,查看'str_replace'。另外:常量?真?在數組中定義? 'define'也會返回一個bool,所以你很可能試着用'array(true)'''preg_replace'' array(true)'...真的:RTFM –

+0

這個問題沒有什麼意義。請顯示實際有效的代碼。很難說這裏有什麼。 – deceze

回答

3

當使用preg_replace時,模式應該被分隔,所以解析器知道模式開始和結束的地方。經常使用的分隔符是正斜槓/

$a = preg_replace(array("/1/", "/3/"), array("10", "30"), "this is a 1 2 3 test"); 

$a現在持有this is a 10 2 30 test

你需要做的是帶分隔符附上模式:

$define_arr = Array ([0] => "/define('APP_TITLE', 'some test')/"); 
$new_value_arr = Array ([0] => "define('APP_TITLE', 'ssfss')"): 


preg_replace($define_arr, $new_value_arr, $fileContent); 

這應該工作。

但是請注意,您並不需要一個數組或preg_replace來完成這個簡單的替換。改爲使用str_replace

+0

$ define_arr = Array([0] =>「/ define('APP_TITLE','some test')/」); 不適用於我,但我將preg_replace更改爲str_replace,這對我的工作感謝 – Duleep

相關問題