Php 路由器::编译
我想了解科哈纳的路由器是如何工作的 我走到方法的编译和面临的困难 这条线是怎么形成的:Php 路由器::编译,php,kohana,kohana-3,Php,Kohana,Kohana 3,我想了解科哈纳的路由器是如何工作的 我走到方法的编译和面临的困难 这条线是怎么形成的: $expression = preg_replace('#'.Route::REGEX_ESCAPE.'#', '\\\\$0', $uri); 方法编译: public static function compile($uri, array $regex = NULL) { // The URI should be considered literal except for ke
$expression = preg_replace('#'.Route::REGEX_ESCAPE.'#', '\\\\$0', $uri);
方法编译:
public static function compile($uri, array $regex = NULL)
{
// The URI should be considered literal except for keys and optional parts
// Escape everything preg_quote would escape except for : ( ) < >
$expression = preg_replace('#'.Route::REGEX_ESCAPE.'#', '\\\\$0', $uri);
if (strpos($expression, '(') !== FALSE)
{
// Make optional parts of the URI non-capturing and optional
$expression = str_replace(array('(', ')'), array('(?:', ')?'), $expression);
}
// Insert default regex for keys
$expression = str_replace(array('<', '>'), array('(?P<', '>'.Route::REGEX_SEGMENT.')'), $expression);
if ($regex)
{
$search = $replace = array();
foreach ($regex as $key => $value)
{
$search[] = "<$key>".Route::REGEX_SEGMENT;
$replace[] = "<$key>$value";
}
// Replace the default regex with the user-specified regex
$expression = str_replace($search, $replace, $expression);
}
return '#^'.$expression.'$#uD';
}
const REGEX_ESCAPE = '[.\\+*?[^\\]${}=!|]';
公共静态函数编译($uri,数组$regex=NULL)
{
//除了键和可选部分之外,URI应该被视为文本
//逃逸所有preg_quote会逃逸的东西,除了:()<>
$expression=preg\u replace(“#”.Route::REGEX\u ESCAPE.“#”、“\\$0”、$uri);
if(strpos($expression,“(”)!==FALSE)
{
//将URI的可选部分设置为非捕获和可选
$expression=str_replace(数组(“(“,”)”),数组(“(?:”,“)?”),$expression);
}
//插入键的默认正则表达式
$expression=str_replace(数组(“”),数组(“(?P'.Route::REGEX_SEGMENT'),$expression);
如果($regex)
{
$search=$replace=array();
foreach($regex作为$key=>$value)
{
$search[]=“”。路由::REGEX_段;
$replace[]=“$value”;
}
//用用户指定的正则表达式替换默认正则表达式
$expression=str_replace($search,$replace,$expression);
}
返回“#^”。$表达式。“$#uD”;
}
const REGEX_ESCAPE='[.\\+*?[^\\]${}=!\124;]';
能不能单独写一篇文章来帮助我理解
// What must be escaped in the route regex
const REGEX_ESCAPE = '[.\\+*?[^\\]${}=!|]';
// The URI should be considered literal except for keys and optional parts
// Escape everything preg_quote would escape except for : ( ) < >
$expression = preg_replace('#'.Route::REGEX_ESCAPE.'#', '\\\\$0', $uri);
要使用反斜杠,需要在regexpr中复制它
使用此preg_替换的几个结果示例:
测试=>测试
测试/=>测试/
//测试//=>//测试/
//test/!=>//test/#
//test/!#$=>//test/#\$
//测试/!\$%^&*aaa()bbb=>//测试/!\$%^&*aaa()bbb
\\\\$0