任何人都可以幫助我解決這個問題。如何使正則表達式變量捕獲路由
example $uri = '/username/carlos'; => $routes[] = '/username/@name';
@name變量$名稱捕獲字符串轉換「卡洛斯」
$routes[] = '/list/edit/@id:[0-9]{3}';
$routes[] = '/username/@name';
$routes[] = '/archive/*';
$routes[] = '/';
$uri = '/username/carlos';
foreach ($routes as $pattern)
{
if (preg_match('#^' . preg_replace('#(?:{{)[email protected](\w+\b)(?:}})?#i', '(?P<\1>[\w\-\.!~\*\'"(),\s]+)',
str_replace('\*', '(.*)', preg_quote($pattern, '/'))) . '\/?$#i', $uri, $matchs))
{
//how to make regex for this to work :
echo $name; // carlos =>$uri = '/username/carlos'; or matt => $uri = '/username/matt';
}
}
感謝閱讀
爲什麼不使用現成的路由框架?你不應該首先分解嵌套模式轉換部分來調試它嗎? – mario