By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,882 Members | 2,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,882 IT Pros & Developers. It's quick & easy.

Remove everything between square brackets.

P: 5

I'm trying to remove square brackets and everything between them from a string, using regular expressions
I know how to use preg_replace, and know the basics of regex, but can't success.
Can anyone please give me a regex for it?

Thanks in advance!
Dec 15 '07 #1
Share this Question
Share on Google+
2 Replies

Expert 5K+
P: 6,050
Post what code you have now and we can improve on that.
Dec 15 '07 #2

P: 5
Okay --

Expand|Select|Wrap|Line Numbers
  1. $string = '1 [fsdfsdfsdf] 2 [fsdfsd] 3';
  2. $string  = preg_replace('/\[(.*)\]/', '', $string); 
  3. echo $string;
Outputs '1 3' for some reason (while it should output 1 2 3).

Unfortunately, I'm not very good with regular expressions and I can't work this out.
Dec 16 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.