PHP regex to replace content between { }

前端 未结 2 604
我在风中等你
我在风中等你 2021-01-26 12:44

I have the following regex to replace everything between [[ and ]] with a callback function. Somehow I don\'t know how to change it to replace the text

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-26 12:54

    Posting an answer that should work with nested brackets also:

    $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
    

    This regex is using conditional subpattern regex.

提交回复
热议问题