简体   繁体   English

php regex vsprintf addign斜杠文件扩展名

[英]php regex vsprintf addign slash on file extension

I have a problem with the following piece of code: pastebin . 我遇到以下代码问题: pastebin For example: 例如:

/^\/index\.php\/index\/home\/(\w+)$/

It adds a slash before the .php extension. 它在.php扩展名之前添加了一个斜杠。 Any ideas how to fix it? 任何想法如何解决它?

Well, if you pass that example as the uri I see that on line 10 you have preg_quote($uri) . 好吧,如果你把这个例子作为uri传递,我在第10行看到你有preg_quote($uri) That should be the reason. 那应该是原因。 Since dot (.) has a meaning in Regex the function is escaping it. 由于dot(。)在Regex中有意义,因此函数正在逃避它。

But that is what you want I believe since if you strip that slash your regex will match ANY character instead of the dot (including the dot). 但这就是你想要的我相信,因为如果剥去斜线,你的正则表达式将匹配任何字符而不是点(包括点)。 So any of these will be valid: 所以这些都是有效的:

indexBphp
index-php
indexmphp
index.php
etc...

Dot in Regex means match any character at this position. 正则表达式中的点表示匹配此位置的任何字符。 So I believe that there is nothing wrong, right? 所以我相信没有错,对吧?

One way to fix this if you still want to have that dot there is to build the regex in two separate parts: 解决这个问题的一种方法是,如果你仍然想要那个点,那就是在两个独立的部分构建正则表达式:

$urlDivided = explode('.php', $url);
$this->finalRegex = preg_quote($urlDivided[0]) . '.php' . preg_quote($urlDivided[1]);

Obviously, the method above assumes that you always have the '.php' extension in the url. 显然,上面的方法假设你在url中总是有'.php'扩展名。 You should do sanity checks. 你应该进行健全性检查。

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

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