-3

I need your help how can I remove anything after and before curly brackets {} in PHP. But I don't want to remove curly brackets and content inside it so if there is anything after of before curly brackets.

I tried this:

 preg_replace('/.[^{]|[^\n{]+.+[^}|}]./', "", $string)

But it doesn't work.

user4035
  • 22,508
  • 11
  • 59
  • 94
Tim
  • 21
  • 3
  • @DipenShah Seems, that he wants to replace after and before and your question is about removing between. – user4035 Aug 10 '15 at 14:59
  • 1
    @Tim Did you try anything? We want to see your efforts. It's a very simple task, that's why many people downvoted. – user4035 Aug 10 '15 at 15:00
  • @user4035 look at OP statement `remove anything after { and before }` which would be the same as `between curly brackets` – Sean Aug 10 '15 at 15:01
  • 1
    @user4035 I am sorry I fail to understand the difference between both. – Dipen Shah Aug 10 '15 at 15:01
  • yes I tried this preg_replace('/.[^{]|[^\n{]+.+[^}|}]./', "", $string) but not – Tim Aug 10 '15 at 15:02
  • @Tim You should have put it into the question. In this case you won't get downvotes. We gave a duplicate question with an answer for you. Does it work? – user4035 Aug 10 '15 at 15:04
  • hello (I want to remove anything from here) {"I am Tim"} (and from here) – Tim Aug 10 '15 at 15:05
  • @DipenShah Seems, that he wants to remove the text on external side of the brackets, not internal. – user4035 Aug 10 '15 at 15:23
  • @Tim I edited your question and nominated it for reopening. – user4035 Aug 10 '15 at 15:23
  • Yes user4035 I want to remove anything outside and I want to have only curly brackets with text inside similarly "here is text symbols that I want ot remove" {"I am Tim"} "I want to remove this text too" and after changes I want only {"I am Tim"} – Tim Aug 10 '15 at 15:27
  • How did you come up with that existing regex then? You could [**edit in**](http://stackoverflow.com/posts/31922822/edit) a few more detailed example strings/variables to highlight what you actually want. (Otherwise you'll perhaps get vague answers. And nobody here is obliged to answer shifting targets). -- You'll probably want to look into exempting `\K` alternative lists to skip `{…}` but remove the rest. Albeit just extracting strings per `preg_match_all()` would be heaps easier. – mario Aug 10 '15 at 16:05

2 Answers2

0

Through this code you can replace anything after and before curly brackets

$string = preg_replace('#\{.*?\}#si', '', $string); 

'

HassanUsman
  • 1,787
  • 1
  • 20
  • 38
  • Hi Hasan, I just tried but it didn't work, it removed all content and left only the closing curly bracket. Thanks – Tim Aug 10 '15 at 15:22
0

This assumes non-nested braces.
Find: [^{}]*(\{[^{}]*\})[^{}]*
Replace: $1

 [^{}]* 
 (                             # (1 start)
      \{
      [^{}]* 
      \}
 )                             # (1 end)
 [^{}]*