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
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.
preg_replace_callback('~\{((?>[^}]++)*)\}~', function ($m) use ($that) {
return "REPLACE TEXT"; }, $layout);