php - Regular expression to take value between brackets "()"? -
i poor in writing regular expression.
i trying value between brackets "()". below...
$a = "polygon((1 1,2 2,3 3,1 1))"; preg_match_all("/\((.*)\)/u", $a, $pat_array); print_r($pat_array);
but give me elements like...
array ( [0] => array ( [0] => ((1 1,2 2,3 3,1 1) ) [1] => array ( [0] => (1 1,2 2,3 3,1 1 ) )
but want fetch "1 1,2 2,3 3,1 1" output.
i know can trim brackets after getting output. great if done using regular expression.
thanks in advance.
solution: per @anubhava answer:
using @anubhava's regular expression.
example:
$a = "polygon((1 1,2 2,3 3,1 1),(1 1,2 2,3 3,1 1),(1 1,2 2,3 3,1 1))";
and if print result of regular expression this...
array ( [0] => array ( [0] => ((1 1,2 2,3 3,1 1) [1] => (1 1,2 2,3 3,1 1) [2] => (1 1,2 2,3 3,1 1)) ) [1] => array ( [0] => 1 1,2 2,3 3,1 1 [1] => 1 1,2 2,3 3,1 1 [2] => 1 1,2 2,3 3,1 1 ) )
look @ second array element, it's want.
fyi: have used fetching geometry data of polygon mysql database , processed array latitude , longitude of polygon nodes.
use negation based regex pattern , result captured group #1:
preg_match_all('/\(+([^()]*)\)+/', $a, $pat_array); print_r($pat_array[1]);
[^()]*
match char not (
or )
.
output:
array ( [0] => 1 1,2 2,3 3,1 1 )
Comments
Post a Comment