我有这个字符串:
的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