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}$~'; 

demo


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

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -