If I have a string like this:
$str = "blah blah blah (a) (b) blah blah blah";
How can I regex so that the output is:
$str = "blah blah blah blah blah blah";
It needs to be able to support any number of bracket pairs inside a string.
This should do the trick:
$str = trim(preg_replace('/\s*\([^)]*\)/', '', $str));
Note, this answer removes whitespace around the bracket too, unlike the other suggestions.
The trim is in case the string starts with a bracketed section, in which case the whitespace following it isn't removed.
Try this:
preg_replace('/\([^)]*\)|[()]/', '', $str)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With