PHP regex to replace content between { }

前端 未结 2 575
我在风中等你
我在风中等你 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.

    0 讨论(0)
  • 2021-01-26 13:03
    preg_replace_callback('~\{((?>[^}]++)*)\}~', function ($m) use ($that) {
    return "REPLACE TEXT"; }, $layout);
    
    0 讨论(0)
提交回复
热议问题