2011-06-10 184 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)