2011-07-17 120 views
2

所以我triing阅读一些PHP代码...我发现这样的行是什么`#((<= )|?&)。OpenID的 [^&] +#`的regexp意思?

$uri = rtrim(preg_replace('#((?<=\?)|&)openid\.[^&]+#', '', $_SERVER['REQUEST_URI']), '?'); 

这是什么意思?如果它(似乎对我来说)只是返回“文件名”为什么如此复杂?

+0

它看起来像是从'REQUEST_URL'中删除'?openid = xx'并将结果放到一个名为'$ uri'的新变量中。 – RobertPitt

回答

10

该行的目的是像openid.something=value从请求URI移除值。

有工具,有以正则表达式转换成散文,有目的,以帮助您了解什么是正则表达式尝试匹配。例如,当你传递给such a tool描述回来为:

NODE      EXPLANATION 
-------------------------------------------------------------------------------- 
    (      group and capture to \1: 
-------------------------------------------------------------------------------- 
    (?<=      look behind to see if there is: 
-------------------------------------------------------------------------------- 
     \?      '?' 
-------------------------------------------------------------------------------- 
    )      end of look-behind 
-------------------------------------------------------------------------------- 
    |      OR 
-------------------------------------------------------------------------------- 
    &      '&' 
-------------------------------------------------------------------------------- 
)      end of \1 
-------------------------------------------------------------------------------- 
    openid     'openid' 
-------------------------------------------------------------------------------- 
    \.      '.' 
-------------------------------------------------------------------------------- 
    [^&]+     any character except: '&' (1 or more times 
          (matching the most amount possible)) 

正如上面所说,正则表达式查找一个?&其次openid.,然后什么也没&。将得到的匹配将包括前述&如果有一个,但不是?由于look behind用于后者。

+0

这是一个很好的工具。 – Joey