2013-12-13 45 views
0

我想替換phpmyadmin ini文件中的配置選項,但沒有被替換。我嘗試了幾種逃避模式和替換的組合,但我無法弄清楚我做錯了什麼。如何用str_replace替換配置指令?

<?php 

$pattern = '$cfg[\'Servers\'][$i][\'auth_type\'] = \'cookie\';'; 

$replacement = '$cfg[\'Servers\'][$i][\'auth_type\'] = \'http\';'; 

$sContents = '$cfg[\'Servers\'][$i][\'auth_type\'] = \'cookie\';'; 

str_replace($pattern, $replacement, $sContents); 
die($sContents); 

這段代碼的輸出是:

$cfg['Servers'][$i]['auth_type'] = 'cookie'; 

什麼是正確的模式和更換,以得到下面的輸出?

$cfg['Servers'][$i]['auth_type'] = 'http'; 

非常感謝您的幫助。

回答

1

這應該工作

$pattern = 'cookie'; 
$replacement = 'http'; 
$return = str_replace($pattern, $replacement, $sContents); 
die($return); 
+0

我很沮喪!沒有看到我忘記將str_replace輸出放在$ sContents變量中-_-' –

0

你不節能的str_replace結果。只要做到這一點,它即使沒有@馬里奧的短版本也能正常工作:

$sContents = str_replace($pattern, $replacement, $sContents); 
die($sContents);