PHP/REGEX: Get a string within parentheses

Macks picture Macks · Jun 28, 2012 · Viewed 27.6k times · Source

This is a really simple problem, but I couldn't find a solution anywhere.

I'm try to use preg_match or preg_match_all to obtain a string from within parentheses, but without the parentheses.

So far, my expression looks like this:

\([A-Za-z0-9 ]+\)

and returns the following result:

3(hollow highlight) 928-129 (<- original string)

(hollow highlight) (<- result)

What i want is the string within parentheses, but without the parentheses. It would look like this:

hollow highlight

I could probably replace the parentheses afterwards with str_replace or something, but that doesn't seem to be a very elegant solution to me.

What do I have to add, so the parentheses aren't included in the result?

Thanks for your help, you guys are great! :)

Answer

Piotr Olaszewski picture Piotr Olaszewski · Jun 28, 2012

try:

preg_match('/\((.*?)\)/', $s, $a);

output:

Array
(
    [0] => (hollow highlight)
    [1] => hollow highlight
)