Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?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);
Copy Clear