Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to remove everything inside braces. For example, if string is:

[hi] helloz [hello] (hi) {jhihi}

then, I want the output to be only helloz.

I am using the following code, however it seems to me that there should be a better way of doing it, is there?

$name = "[hi] helloz [hello] (hi) {jhihi}";
$new  =  preg_replace("/\([^)]+\)/","",$name); 
$new = preg_replace('/\[.*\]/', '', $new);  
$new = preg_replace('/\{.*\}/', '', $new);  
echo $new;
like image 335
Vishnu Avatar asked Nov 13 '13 08:11

Vishnu


1 Answers

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.

like image 97
Damien Overeem Avatar answered Sep 21 '22 23:09

Damien Overeem