I have a string that has the format of routes:
/foo/
/{bar}/
/foo/{bar}
/{foo}/{bar}
/{foo}/bar/
Can have many values between slashes, values between {}
are variable
I wanted to catch all occurrences of {qualquercoisa}
of these strings, for example:
/foo/{bar} => ["bar"]
/foo/bar => []
/{foo}/{bar}/ => ["foo", "bar"]
I tried with preg_match
, but could only capture one occurrence:
preg_match("/\{([^\/]+)\}/", "/foo/{bar}/{baz}/", $match);
//$match = ["{bar}", "bar"]
preg_match("/(?:\{([^\/]+)\}\/?|[^\/]+\/?)+/", "/foo/{bar}/{baz}/", $match);
//$match = ["foo/{bar}/{baz}/", "baz"]
How to capture all occurrences?