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?
 
ddrudikConnect With a Mentor Commented:
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
 
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
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
 
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:
Thanks for the question and the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.