- var_dump: documentation ( source)
- preg_match: documentation ( source)
<?php
function extractRouteParameterComponents($routeParameter) {
$regexPattern = "/^{(\w+)(?::(.+))?}(\?)?$/";
if (preg_match($regexPattern, $routeParameter, $matches)) {
$name = $matches[1];
$type = $matches[2] ?? "[^/]";
$optional = isset($matches[3]);
return [$name, $type, $optional];
}
}
$routeParameter = "{id:[0-9]+}";
$parameterComponents = extractRouteParameterComponents($routeParameter);
var_dump($parameterComponents);