Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
$pattern = "/{context}/{alias?}";
$uri = "uk";
$regex = '#^' . preg_replace(
['#\{([a-zA-Z0-9_]+)\?\}#', '#\{([a-zA-Z0-9_]+)\}#'],
['/?([^/]*)?', '/?([^/]+)'], // Добавил /? чтобы alias был optional
trim($pattern, '/')
) . '/?(/?)$#'; // Добавил /? в конце
// Убираем лишние слэши, если они есть
$regex = str_replace('//', '/', $regex);
var_dump($regex); // Показываем, как выглядит финальная регулярка
var_dump($uri); // Проверяем, что в $uri
$request = [];
if (preg_match($regex, $uri, $matches)) {
array_shift($matches);
$matches = array_filter($matches, 'trim');
print_r($matches);
print_r(array_merge($matches, [$request]));
}