PHP preg_replace string - contents in parentheses

I am trying to understand PHP preg_replaceand was wondering if you can offer any recommendations on how to line the word in brackets, but remove everything else from this line:

Events (Road)

So he will return:

Road

I really want to learn, so I do not just need an answer, but I need to understand how this is possible.

I know how to remove words in brackets (and brackets) with

trim(preg_replace('/\s*\([^)]*\)/', '', 'Events (Road)')

Cheers r

+4
source share
1 answer

One way is to grab the characters in parentheses, and then replace everything else with this. $1is a link back to the first capture group ():

preg_replace('/.*\(([^)]*)\)/', '$1', 'Events (Road)');

Regular expression visualization

Demo version of Debuggex

+3
source

All Articles