php - Exclude files using a regular expression -
i want exclude .mp3
, .jpeg
files (with regular expression, not php). know !preg_match
)
my example below matches:
$str = 'file.mp3'; // exclude $ex = '~(?!\.(mp3|jpe?g))$~'; if (preg_match($ex, $str)) { echo "match!"; } else { echo "nothing match!"; }
your negative lookahead isn't working because there nothing ahead at. remember lookaround assertions zero-width — not consume characters. still need account filename extension characters.
change expression follows:
$ex = '~(?!\.(mp3|jpe?g))[a-z]{3,4}$~';
a better approach use pathinfo()
though. maintain array of extensions you'd disallow , use in_array()
check if extension of filename in array:
$disallowed = ['mp3', 'jpg', 'jpeg', /* more extensions */ ]; if (in_array(pathinfo($str, pathinfo_extension), $disallowed)) { # code... }
Comments
Post a Comment