I have the following:
(hello) (hello123) (hello123$pecialChars&开发者_运维技巧amp;#@)
I need a way to get the contents between the parenthesis each time. What would be a good way of doing that?
Well since there aren't spaces in each ( ), the following pattern should work \(([^ ]+)\)/
(match one or more of anything that isn't a space and is between parenthesis, which are escaped to be literal characters):
$data = "(hello) (hello123) (hello123$pecialChars&#@)";
preg_match_all('/\(([^ ]+)\)/', $data, $arr, PREG_PATTERN_ORDER);
// print_r($arr) gives:
Array
(
[0] => Array
(
[0] => (hello)
[1] => (hello123)
[2] => (hello123$pecialChars&#@)
)
[1] => Array
(
[0] => hello
[1] => hello123
[2] => hello123$pecialChars&#@
)
)
Edit: As mentioned, the pattern \(([^)]+)\)
, or match an open parenthesis followed by one or more characters that are not a close parenthesis and are followed by a close parenthesis
, might be better (depending on if you might have a closing parenthesis in your data or if you might have spaces).
精彩评论