3v4l.org

run code in 500+ PHP versions simultaneously
<?php function getRegexUri(string $uri): string { return '#^' . \preg_replace_callback( '/\/\{(\??)([a-zA-Z_][a-zA-Z0-9_]*)(?::([^}]+)|<([^>]+)>)?}/', function (array $matches): string { $regex = $matches[3] ?? null; $type = $matches[4] ?? null; if ($regex === '') { $regex = null; } $pattern = $regex ?? $this->patterns->get($type ?? '')->regex ?? '[^/]+'; $segment = '(?<' . $matches[2] . '>' . $pattern . ')'; return $matches[1] === '?' ? '(?:/' . $segment . ')?' : '/' . $segment; }, $uri, ) . '$#'; } $v = '/test-http-{code:\d+}/'; var_dump(getRegexUri($v));

preferences:
69.46 ms | 2704 KiB | 5 Q