I am trying to create an URL matching pattern where the route parameters can be read.
This is what I have:
$routePattern = '/test/{id}/edit';
// Can I strip the opening and closing bracket from `$0` here?
$regexPattern = '#^' . preg_replace('#{[\w]+}#', '(?P<$0>[\w]+)', $routePattern) . '$#';
// Matching done here...
The problem is that this will result in: #^test/(?P<{id}>[\w]+)/edit$#
.
But I would like that the brackets get stripped from id
. So I would like the following result: #^test/(?P<id>[\w]+)/edit$#
.
How is this possible in a clean way? This is the non clean way I found:
$routePattern = '/test/{id}/edit';
$regexPattern = '#^' . preg_replace('#{[\w]+}#', '(?P<$0>[\w]+)', $routePattern) . '$#';
$regexPattern = str_replace(['{', '}'], '', $regexPattern);
// Matching done here...