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
//// Примеры маршрутов ////
// $path = '/news';
// $path = '/product/12';
// $path = '/profile/12/delete';
$path = '/news/music/page/2';
//// Шаблоны ////
$templates = [
'/news/:category/page/:page' => [
'regexp' => '@/news/(.+)/page/(\d+)@',
'args' => ['category', 'page'],
'file' => 'Новости со страницей',
]
];
$route = [
'file' => 'not found'
];
// Перебираем все шаблоны маршрутов
foreach ($templates as $template) {
if (preg_match($template['regexp'], $path, $matches)) {
print_r($matches);
array_shift($matches);
$route = [
'file' => $template['file'],
'args' => array_combine($template['args'], $matches)
];
break;
}
}
print_r($route);