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
so
$string = 'this is the text (hello all)';
this is the text
Before you finalize the question, you might consider:
echo preg_replace('/\([^)]*\)/' ,'',$strin g);
Or to remove any spaces (horizontal/veritcal/etc.) before the first parens:
echo preg_replace('/\s*\([^)]*\ )/','',$so urcestring );
echo preg_replace('/\([^)]*\)/'
Or to remove any spaces (horizontal/veritcal/etc.)
echo preg_replace('/\s*\([^)]*\
ASKER
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_rep lace:"/([a -z0-9_\-]+ )\s+\(.+\) /i":"\\1"}
can yours work in this scenario?
{$thedata.rtitle|regex_rep
can yours work in this scenario?
I don't have a way of testing that, I guess you could try:
{$thedata.rtitle|regex_rep lace:"/\s* \([^)]*\)/ ":""}
And let me know if that worked for you.
{$thedata.rtitle|regex_rep
And let me know if that worked for you.
ASKER
Yes, that did actually work.
Is that a better approach than the one I pasted. Can you explain the difference between the 2?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks for the question and the points.
ASKER
$str = "not between brackets (between brackets)";
echo preg_replace("/([a-z0-9_\-