Finding and removing brackets and the text between brackets?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Finding and removing brackets and the text between brackets?

Post by mikebr »

I am trying to strip text and brackets for strings, for example:

'Location name (MA)' should become 'Location name', I can't use a simple str_replace() as the chars between the brackets could be any combination of 1 to 3 letters in caps from A-Z, anyone know how I can do this easily?

Many thanks
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

regular expressions seem like the best way to go

something like:

Code: Select all

preg_replace("#(\s?\([A-Z]{2,3}\))#",'');
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Post by mikebr »

I pull my hair out with these things, that did the trick and seems to work well.

Thanks
Post Reply