2011-06-10 207 views
0

如何更改以下preg_match()代码以阻止index.html文件,而不是仅替换'。'。 ,'..'或'.htaccess'?正确使用preg_match?

preg_match('/^\.(htaccess|\.)?/',$file) 

回答

3

这应该这样做

preg_match('/^(\.(htaccess|\.)?|index\.html)/',$file) 

然而,正则表达式是没有必要在这里

in_array($file, array('.', '..', '.htaccess', 'index.html')) 

更容易阅读和扩展:)

+0

感谢,感激 – Jason 2011-06-10 21:58:38

0

这会是这样的:

preg_match('/^(\.(htaccess|\.)?)|(index\.html)/',$file) 

顺便说一句,括号只是为了清楚。

0

其实preg_match('/^\.(htaccess|\.)?/',$file)也阻止任何其他文件以.开头(因为?)。所以KingCrunch的第二个解决方案是不一样的,可能是有风险的!

所以,当你想匹配的任何文件以点开始(因此也包括.htaccess..)和index.html你可以使用:

preg_match('/^(\.|index\.html)/',$file)