Regex between brackets

I just need to use regex to remove allbetween the brackets and the brackets themselves.

so

$string = 'this is the text (hello all)';

this is the text


lexshineAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lexshineAuthor Commented:
can I start awarding myself 500 points??

$str = "not between brackets (between brackets)";
echo preg_replace("/([a-z0-9_\-]+)\s+\(.+\)/i", "\\1", $str);
0
ddrudikCommented:
Before you finalize the question, you might consider:
echo preg_replace('/\([^)]*\)/','',$string);
Or to remove any spaces (horizontal/veritcal/etc.) before the first parens:
echo preg_replace('/\s*\([^)]*\)/','',$sourcestring);
0
lexshineAuthor Commented:
Well one thing I didnt mention is it needed to work in a smarty template parse statement which the one I posted does.

{$thedata.rtitle|regex_replace:"/([a-z0-9_\-]+)\s+\(.+\)/i":"\\1"}

can yours work in this scenario?
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

ddrudikCommented:
I don't have a way of testing that, I guess you could try:
{$thedata.rtitle|regex_replace:"/\s*\([^)]*\)/":""}

And let me know if that worked for you.
0
lexshineAuthor Commented:
Yes, that did actually work.

Is that a better approach than the one I pasted. Can you explain the difference between the 2?

0
ddrudikCommented:
My pattern finds the () groups (and any spaces before them) and replaces what's found with an empty string.  In general, the shorter and less complex (fewer match groups etc.) the patterns, the more efficient.
0
ddrudikCommented:
Here's a full explanation of my pattern:
NODE                     EXPLANATION
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  \(                       '('
----------------------------------------------------------------------
  [^)]*                    any character except: ')' (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  \)                       ')'
----------------------------------------------------------------------

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ddrudikCommented:
Thanks for the question and the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.