Link to home
Start Free TrialLog in
Avatar of lexshine
lexshine

asked on

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


Avatar of lexshine
lexshine

ASKER

can I start awarding myself 500 points??

$str = "not between brackets (between brackets)";
echo preg_replace("/([a-z0-9_\-]+)\s+\(.+\)/i", "\\1", $str);
Avatar of ddrudik
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);
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?
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.
Yes, that did actually work.

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

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.
ASKER CERTIFIED SOLUTION
Avatar of ddrudik
ddrudik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the question and the points.