2012-04-29 46 views
1

您好我想在行在此使用preg_replace?

$key = 'ABC'; 

我寫這個PHP代碼來代替單引號內的文字,:

$data = '$key = \'ABC\''; 
preg_match($data, '/s$key = \'(.*)\'\;/s', $security_key); 
print_r($security_key); 

,但它給像警告警告:的preg_match()[function.preg-匹配]:沒有結局匹配的分隔符 '>'

+0

使用str_replace函數如果你唯一的替換字符串http://php.net/manual/en/function.str-replace.php – Philip 2012-04-29 05:13:56

回答

2
$data = '$key = \'ABC\''; 
$replacement = 'CBA'; 
$data = preg_replace("/'[^']*'/", "'$replacement'", $data); 
var_dump($data); 
0

正則表達式必須開始,以delimiter結束 - %,/,$,#,〜...

試試這個:

$data = '%$key = \'ABC\'%'; 
preg_match($data, '/s$key = \'(.*)\'\;/s', $security_key); 
print_r($security_key);