简体   繁体   English

使用preg_match_all()函数验证RewriteRule行

[英]Validating a RewriteRule line using preg_match_all() function

I am trying to validate a RewriteRule line using preg_match_all(), 我正在尝试使用preg_match_all()验证RewriteRule行,

Here is the line I am trying to validate to : 这是我要验证的行:

RewriteRule ^(.*)$ /edt.php

and this is my regex pattern so far : 到目前为止,这是我的正则表达式模式:

RewriteRules\s+[^s]+\s+[^s]+\s+([NC,NE,L,R])?/i

The last argument [NC,NE,L,R] is optional in the line. 该行中的最后一个参数[NC,NE,L,R]是可选的。

My complete code with php 我用PHP完成的代码

$x="RewriteRule ^(.*)$ /edt.php ";

if(preg_match_all("/RewriteRules\s+[^s]+\s+[^s]+\s+([NC,NE,L,R])?/i",$x,$m))
{echo "Line is ok";}
else
{echo "There is something wrong with the line";}

This is not working, I am getting the "else" part of the code in my output. 这是行不通的,我在输出中得到了代码的“ else”部分。 I believe there is something wrong with the pattern , I have already wasted 20mnts trying to solve it by myself, but I could not find where exectly the fault is. 我相信该模式有问题,我已经浪费了20百万次尝试自己解决它,但是我找不到错误所在。

Any help is much appriciated. 任何帮助都非常有用。

There's a lot I don't understand about this question, but I will do what I can to try and help you out here. 关于这个问题,我有很多我不了解的地方,但是我会尽力帮助您。

Let's start with your string: 让我们从您的字符串开始:

RewriteRule ^(.*)$ /edt.php

This can be matched a number of ways, but I will just give you a general approach to doing it and show you a couple of potential errors that you have. 这可以通过多种方式进行匹配,但是我将为您提供一种通用的实现方法,并向您展示一些潜在的错误。

It as though you are just matching a pattern like this: 好像您只是在匹配这样的模式:

RewriteRule SPACE SOMETHING SPACE SOMETHING SPACE OPTIONAL_DATA_WITH BRACKETS
      1       2       3       4       5       6                 7

Here is a regular expression that matches this: 这是与此匹配的正则表达式:

^RewriteRule  \s+  .*?  \s+  .*?  (?:$|\s+  \[[A-Z,]+\])
     1         2    3    4    5       6           7   

I have added in whitespace to my expression simply to make it easier to read. 我在表达式中添加了空格,只是为了使其更易于阅读。 There should be no actual space characters in this expression. 此表达式中不应有实际的空格字符。

  1. ^RewriteRule Simply matching the phrase. ^RewriteRule只需匹配短语。 We use the ^ to anchor it to the beginning of the line. 我们使用^将其锚定到行的开头。
  2. \\s+ Requiring at least one space before the next item. \\s+在下一项之前至少需要一个空格。 This is already what you are using. 这已经是您正在使用的。
  3. .*? This is kind of a catch-all that can be refined down to whatever specifics you wish. 这是一个万能的,可以细化为您想要的任何细节。 But for this purpose, I am just saying to match any character . 但是为此,我只是说要匹配任何字符. , any number of times * , until you hit the next item to match ? 多少次* ,直到您找到下一个要匹配的项目? . In this case, the next item is a whitespace character. 在这种情况下,下一项是空白字符。
  4. \\s+ Requiring at least one space before the next item \\s+在下一项之前至少需要一个空格
  5. .*? Matching any character, any number of times until the next item. 匹配任何字符,任意次数,直到下一项。
  6. (?:$|\\s+ This may look like a mess, but it's really just matching either the end of the line or a whitespace character. (?: is made up of two parts that just says to start a new group ( , but don't remember it ?: . I am following that with a line ending $ or | a space \\s+ . (?:$|\\s+这看起来像是一团糟,但实际上只是匹配行尾或空白字符。 (?: ?:由两部分组成,它们只是说要开始一个新的组( ,但不记得了?: 。我在后面跟随以$|结尾的行\\s+
  7. \\[[AZ,]+\\]) This last bit of the expression is simply matching a literal opening square brace [ , followed by a character class consisting of letters or a comma [AZ,] and requiring at least one of them to be present + . \\[[AZ,]+\\])表达式的最后一点是简单地匹配一个文字右方括号[ ,然后是一个由字母或逗号[AZ,]组成的字符类[AZ,]并要求至少其中一个为现在+ I'm closing it out with an ending square brace ] and the closing parenthesis ) for the "or". 我用一个结尾的方括号[ ]和“或”的右括号( )封闭了它。

This can be fine-tuned quite a bit, but here is a demo for you to play around with: 可以对它进行微调,但是这里有一个演示供您试用:

https://regex101.com/r/qF7bP6/1 https://regex101.com/r/qF7bP6/1

Errors: 错误:

You have a couple of items that I can tell right away are going to throw off your match. 我有几个可以立即告诉您的项目将使您的比赛失败。

I can see that you are checking for RewriteRules instead of RewriteRule . 我可以看到您正在检查RewriteRules而不是RewriteRule

Additionally, you are matching the RewriteRule Flags in square braces like this [NC,NE,L,R] . 此外,您正在像这样[NC,NE,L,R]方括号中匹配RewriteRule标志。 Square braces are special characters in regular expressions, so you're actually attempting to matching an optional single character which must be either a comma or one of the following letters: N , C , E , L or R . 方括号在正则表达式中是特殊字符,因此您实际上是在尝试匹配可选的单个字符,该字符必须是逗号或以下字母之一: NCELR

Finally, you are requiring a space after the URL, even if there is no RewriteRule flag present. 最后,即使没有RewriteRule标志,您也需要在URL后留一个空格。 There may be more, but those are the ones that jumped out at me. 可能还有更多,但是那些是跳到我身上的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM