0

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 between single curly braces { }:

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

2 Answers 2

3
preg_replace_callback('~\{((?>[^}]++)*)\}~', function ($m) use ($that) {
return "REPLACE TEXT"; }, $layout);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.