PHP正则表达式替换{}之间的内容

时间:2014-01-20 07:18:29

标签: php regex

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

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分: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