2014-01-20 82 views
0

之间的内容我有以下正则表达式来替换[[]]之间的所有内容和回调函数。不知怎的,我不知道如何去改变它来代替{ }单花括号之间的文字:PHP正则表达式来替换{

preg_replace_callback('~\[\[((?>[^]]++|](?!]))*)]]~', function ($m) use ($that) { 
    return "REPLACE TEXT"; }, $layout); 

回答

3
preg_replace_callback('~\{((?>[^}]++)*)\}~', function ($m) use ($that) { 
return "REPLACE TEXT"; }, $layout); 
+0

再试一次,编辑它。 http://regex101.com/r/qN3sE7 –

+0

谢谢!像魅力一样工作! +1 –

+0

这也适用于嵌套括号吗? – anubhava

1

发布的答案应该与嵌套的括号也行:

$str = 'this is the text that {i want{to} replace this text} from it'; 
echo preg_replace('/ \{ ((?: [^{}]* | (?0))+) \} /x', 'REPLACE TEXT', $str); 
//=> this is the text that REPLACE TEXT from it 

此正则表达式正在使用conditional subpattern regex