Properly use preg_match?
How would I change the following preg_match() code to also block the index.html file, stead of only '.' , '..' , or '.htaccess'?
preg_ma开发者_开发问答tch('/^\.(htaccess|\.)?/',$file)
This should do it
preg_match('/^(\.(htaccess|\.)?|index\.html)/',$file)
However, regular expressions are not necessary here
in_array($file, array('.', '..', '.htaccess', 'index.html'))
More readable and easier to extend :)
That would be something like:
preg_match('/^(\.(htaccess|\.)?)|(index\.html)/',$file)
By the way, the parenthesis are just for clarity.
Actually preg_match('/^\.(htaccess|\.)?/',$file)
blocks also any other file beginning with a .
(because of the ?
). So KingCrunch's second solution is not the same and can be risky!
So when you want to match any file starting with a dot (so also including .htaccess
and ..
) and index.html
you could use:
preg_match('/^(\.|index\.html)/',$file)
精彩评论