我有這個字符串:
的preg_replace [{和}]
$string='My String [{Text_001}] My String [{Text_002}] ';
我想用的preg_replace [{事}]由$的東西(如更換:[{Text_001}] - > $ Text_001 )。
我有我的preg_replace錯誤:
$string = preg_replace('/([{.+?)+(}])/i', "$1", $string);
我有這個字符串:
的preg_replace [{和}]
$string='My String [{Text_001}] My String [{Text_002}] ';
我想用的preg_replace [{事}]由$的東西(如更換:[{Text_001}] - > $ Text_001 )。
我有我的preg_replace錯誤:
$string = preg_replace('/([{.+?)+(}])/i', "$1", $string);
$string = preg_replace('~\[\{(.+?)\}\]~', "\$$1", $string);
# My String $Text_001 My String $Text_002
\[
and {
have a special meaning in PCRE(加上你有畸形的正則表達式)。只是逃避他們:
$string = preg_replace('/(\\[\\{(.+?)\\}\\])/i', "$1", $string);
用途:
<?php
$string='My String [{Text_001}] My String [{Text_002}] ';
$string = preg_replace("~".preg_quote('[{')."(.*?)".preg_quote('}]')."~","something", $string);
echo $string;
?>
輸出更換後[{something}]
通過something
:
My String something My String something