PHP/REGEX: Get a string within parentheses
Asked Answered
P

2

8

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! :)

Potentilla answered 28/6, 2012 at 16:56 Comment(2)
What you're calling brackets are actually parentheses.Catamite
Possible duplicate of PHP: Best way to extract text within parenthesis?Fertilizer
P
18

You just need to add capturing parenthesis, in addition to your escaped parenthesis.

<?php
    $in = "hello (world), my name (is andrew) and my number is (845) 235-0184";
    preg_match_all('/\(([A-Za-z0-9 ]+?)\)/', $in, $out);
    print_r($out[1]);
?>

This outputs:

Array ( [0] => world [1] => is andrew [2] => 845 ) 
Peak answered 28/6, 2012 at 17:44 Comment(4)
Thanks for your answer. This works great (besides the escaped backslashes). But how do brackets help? I thought my expression was read like this: "an open bracket followed by amount of chars that consist of letters, numbers and spaces, followed by a closed bracket". How do the (non-escaped-)brackets come into play here? Thank you for your help! :)Potentilla
Oops, sorry about the escaped backslashes; fixed now. Non-escaped brackets have a special meaning: capture. See, by default, the entire match is captured into [0], which is why $out[0] contains everything, even the brackets you don't want. Each () capture, well, captures whatever's in between them, and stores them in sequential array indices, i.e. [1], [2], [3], etc. For example, if your string is "abc123def456" and your regex is /abc(\d+)(\w+)(\d)(\d+)/, your captures would be like so: [1] = "123", [2] = "def", [3] = "4", and [4] = "56".Peak
The difference between preg_match and preg_match_all is that the latter will, as the name suggests, match all, so that there will be an array within the array: [1] = { "world", "is andrew", "845" }.Peak
Thank you acheong87. I tried reading it up myself, but I didn't get it. Your explanation however was great, I understand now. Thank you very much! :)Potentilla
W
26

try:

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

output:

Array
(
    [0] => (hollow highlight)
    [1] => hollow highlight
)
Winston answered 28/6, 2012 at 16:59 Comment(0)
P
18

You just need to add capturing parenthesis, in addition to your escaped parenthesis.

<?php
    $in = "hello (world), my name (is andrew) and my number is (845) 235-0184";
    preg_match_all('/\(([A-Za-z0-9 ]+?)\)/', $in, $out);
    print_r($out[1]);
?>

This outputs:

Array ( [0] => world [1] => is andrew [2] => 845 ) 
Peak answered 28/6, 2012 at 17:44 Comment(4)
Thanks for your answer. This works great (besides the escaped backslashes). But how do brackets help? I thought my expression was read like this: "an open bracket followed by amount of chars that consist of letters, numbers and spaces, followed by a closed bracket". How do the (non-escaped-)brackets come into play here? Thank you for your help! :)Potentilla
Oops, sorry about the escaped backslashes; fixed now. Non-escaped brackets have a special meaning: capture. See, by default, the entire match is captured into [0], which is why $out[0] contains everything, even the brackets you don't want. Each () capture, well, captures whatever's in between them, and stores them in sequential array indices, i.e. [1], [2], [3], etc. For example, if your string is "abc123def456" and your regex is /abc(\d+)(\w+)(\d)(\d+)/, your captures would be like so: [1] = "123", [2] = "def", [3] = "4", and [4] = "56".Peak
The difference between preg_match and preg_match_all is that the latter will, as the name suggests, match all, so that there will be an array within the array: [1] = { "world", "is andrew", "845" }.Peak
Thank you acheong87. I tried reading it up myself, but I didn't get it. Your explanation however was great, I understand now. Thank you very much! :)Potentilla

© 2022 - 2024 — McMap. All rights reserved.