Need to delete brackets and text in it. For now, it almsot working. Deletes all text starting from bracket. My code:
$lines = preg_replace("/\(([^\d]+)/", '', $lines);
For example, text:
Some random words (word1 / word2 / word3) aaaa
Code deletes all text after brackets too,
Some random words
but what I need to do is (what it should look like):
Some random words aaaa
/\([^()]*\)/
\( - opening bracket[^()]* - as many characters as possible, which are not brackets\) - closing bracketYou can optionally remove one space at the end (/\([^()]*\) ?/) so that you wont have two spaces where the removal happened.
Note that this wont handle cases of nested brackets like:
foo (bar (baz) quiz)
To do that, you will have to be a little more creative:
(?<no_brackets>[^()]*){0}(?<balanced_brackets>\(\g<no_brackets>\)|\(\g<no_brackets>\g<balanced_brackets>\g<no_brackets>\))
See it in action
You are missing an extra \) at the end of your expression. As is, your expression will start matching the moment it finds an open bracket and keep on going.
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