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
$uri = 'uk';
$pattern = '/{context}{alias?}';
$regex = preg_replace('#\{([a-zA-Z0-9_]+)\?\}#', '(?:/([^/]+))?', $pattern);
$regex = preg_replace('#\{([a-zA-Z0-9_]+)\}#', '/([^/]+)', $regex);
$regex = '#^' . $regex . '$#';
$regex = preg_replace('#/{2,}#', '/', $regex);
preg_match($regex, $uri, $uriMatches);
// Приводим массив к ассоциативному виду
$keys = ['context', 'alias'];
$values = array_slice($uriMatches, 1);
$uriMatches = array_combine($keys, array_pad($values, count($keys), null));
print_r($uriMatches);