phpregexstringpreg-replacecurly-braces

Remove curly brace wrapped placeholders in a string


I have a string like this {\i1}You were happy?{\i0} and I want to remove all {...} so that just the text You were happy? is left.

I have tried it with some regex patterns, but I did not get it work.

One of my try:

$text = preg_replace("/{.*}/", "\\0", $text);

Solution

  • You can use this for replacing all the text between {}

    $result = preg_replace('/\{[^}]*\}/', '', $subject);

    Explanation

    "
    \{      # Match the character “{” literally
    [^}]    # Match any character that is NOT a “}”
       *       # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
    \}      # Match the character “}” literally
    "