これは、単純な文字列置換を呼び出す正規表現で実現できます。
function replaceInsideBraces($match) {
return str_replace('@', '###', $match[0]);
}
$input = '{{ some text @ other text @ and some other text }} @ this should not be replaced {{ but this should: @ }}';
$output = preg_replace_callback('/{{.+?}}/', 'replaceInsideBraces', $input);
var_dump($output);
私はあなたのブレースを見つけるために単純な貪欲でない正規表現を選びました、しかしあなたはパフォーマンスのためにまたはあなたのニーズに合うようにこれを変えることを選ぶかもしれません。
匿名関数を使用すると、置換をパラメーター化できます。
$find = '@';
$replace = '###';
$output = preg_replace_callback(
'/{{.+?}}/',
function($match) use ($find, $replace) {
return str_replace($find, $replace, $match[0]);
},
$input
);
ドキュメント: http: //php.net/manual/en/function.preg-replace-callback.php