开发者

What does this `#((?<=\?)|&)openid\.[^&]+#` regexp mean?

开发者 https://www.devze.com 2023-03-21 00:58 出处:网络
So I am triing to read some php code开发者_如何学JAVA... I found such line $uri = rtrim(preg_replace(\'#((?<=\\?)|&)openid\\.[^&]+#\', \'\', $_SERVER[\'REQUEST_URI\']), \'?\');

So I am triing to read some php code开发者_如何学JAVA... I found such line

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

what does it mean? and if it (seems for me) just returns 'file name' why it is so complicated?


The purpose of that line is to remove values like openid.something=value from the request URI.

There are tools out there to translate regex into prose, with an aim to help you understand what a regex is trying to match. For example, when yours is passed to such a tool the description comes back as:

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))

As the above says, the regex looks for a ? or & followed by openid., followed by anything not &. The resulting match will include the preceeding & if there is one, but not the ? since a look behind was used for the latter.

0

精彩评论

暂无评论...
验证码 换一张
取 消