How to replace everything between {} [] () braces from a string?

This should work:

$name = "[hi] helloz [hello] (hi) {jhihi}";
echo preg_replace('/[\[{\(].*?[\]}\)]/' , '', $name);

Paste it somewhere like: http://writecodeonline.com/php/ to see it work.


[old answer]

If needed, the pattern that can deal with nested parenthesis and square or curly brackets:

$pattern = '~(?:(\()|(\[)|(\{))(?(1)(?>[^()]++|(?R))*\))(?(2)(?>[^][]++|(?R))*\])(?(3)(?>[^{}]++|(?R))*\})~';

$result = preg_replace($pattern, '', $str);

[EDIT]

A pattern that only removes well balanced parts and that takes in account the three kinds of brackets:

$pattern = '~
    \[ [^][}{)(]*+ (?: (?R) [^][}{)(]* )*+  ]
  |
    \( [^][}{)(]*+ (?: (?R) [^][}{)(]* )*+ \)
  |
    {  [^][}{)(]*+ (?: (?R) [^][}{)(]* )*+  }
~xS';

This pattern works well but the additional type check is a bit overkill when the goal is only to remove bracket parts in a string. However it can be used as a subpattern to check if all kind of brackets are balanced in a string.


A pattern that removes only well balanced parts, but this time, only the outermost type of bracket is taken in account, other types of brackets inside are ignored (same behavior than the old answer but more efficient and without the useless conditional tests):

$pattern = '~
    \[ ( [^][]*+ (?: \[ (?1) ] [^][]* )*+ )  ]
  |
    \( ( [^)(]*+ (?: \( (?2) ] [^)(]* )*+ ) \)
  |
     { ( [^}{]*+ (?:  { (?3) } [^}{]* )*+ )  }
~xS';