简体   繁体   English

PHP数组值到正则表达式模式

[英]php array values to regex pattern

So I have an array with values, this case file extentions but i'm using preg_match to filter out file extentions that are not allowed. 所以我有一个带有值的数组,这种情况下是文件扩展名,但是我正在使用preg_match来过滤掉不允许的文件扩展名。

Is there an easy way to translate an array to an regex pattern? 有没有简单的方法可以将数组转换为正则表达式模式?

Source array 源数组

(
  [1] => 'file-to-path/thumbs/allowedfile.pdf',
  [2] => 'file-to-path/thumbs/notallowedfile.psd',
  [3] => 'file-to-path/project/test/notallowedfile.txt',
)

Allowed extentions array 允许扩展数组

( 
    [0] => bmp
    [1] => jpg
    [2] => jpeg
    [3] => gif
    ...
)

The code that im using right now. 我现在正在使用的代码。

foreach($array as $key => $val){

     if( !preg_match('/^.*\.(jpg|png|jpeg|bmp|gif|pdf)$/i', $val ) ){

           // this part needs to be an array
           '/^.*\.(jpg|png|jpeg|bmp|gif|pdf)$/i' -> [array with the values]

      }

}

I would avoid regex for this, and use pathinfo($value, PATHINFO_EXTENSION) and in_array() 我会避免使用正则表达式,而使用pathinfo($value, PATHINFO_EXTENSION)in_array()

For example, which populates an $error array and filters your array. 例如,它将填充$error数组并过滤您的数组。

<?php
$allowed_ext = array ( 
    'bmp',
    'jpg',
    'jpeg',
    'gif',
    'pdf'
);

$files = array(
  1 => 'file-to-path/thumbs/allowedfile.pdf',
  2 => 'file-to-path/thumbs/notallowedfile.psd',
  3 => 'file-to-path/project/test/notallowedfile.txt'
);

$errors = [];

foreach ($files as $key => $value) {
    if (!in_array(pathinfo($value, PATHINFO_EXTENSION), $allowed_ext)) {
        $errors[] = basename($value).' is not allowed.';
        unset($arr[$key]);
    }
}

print_r($errors);
print_r($files);

https://3v4l.org/oSOJT https://3v4l.org/oSOJT

Result: 结果:

Array
(
    [0] => notallowedfile.psd is not allowed.
    [1] => notallowedfile.txt is not allowed.
)
Array
(
    [1] => file-to-path/thumbs/allowedfile.pdf
)

If you just want to filter the array, then you can use array_filter() 如果只想过滤数组,则可以使用array_filter()

$files = array_filter($files, function ($file) use ($allowed_ext) {
   return in_array(pathinfo($file, PATHINFO_EXTENSION), $allowed_ext);
});

You can use array_filter function then check the extension of a single file in the list of available extensions, and return true if it's valid extension otherwise returns false and it's exclude. 您可以使用array_filter函数,然后在可用扩展名列表中检查单个文件的扩展名,如果有效扩展名则返回true,否则返回false并排除。

$validFiles = array_filter($files, function ($file) use ($key, $value, arrOfValidExtensions) {
   $fileExtenstion = getFileExtension($value); //Implement this function to return the file extension
   return in_array($fileExtension, $arrOfValidExtensions);
});

all not allowed files you can quick ignore them with minimum cpu use 所有不允许的文件,您可以使用最少的CPU使用率来快速忽略它们

 <?php function check(){ $who = array( 1 => 'file-to-path/thumbs/allowedfile.pdf', 2 => 'file-to-path/thumbs/notallowedfile.psd', 3 => 'file-to-path/project/test/notallowedfile.txt' ); return preg_grep('/^.*\\.(jpg|png|jpeg|bmp|gif|pdf)$/i', $who ); } print_r(check()); ?> 

in my browser i got this push Run Code Snippet: 在我的浏览器中,我得到了这个推送运行代码片段:

 Array ( [1] => file-to-path/thumbs/allowedfile.pdf ) 

or 要么

 <?php function check($who){ return preg_grep('/^.*\\.(jpg|png|jpeg|bmp|gif|pdf)$/i', $who ); } print_r(check(array( 1 => 'file-to-path/thumbs/allowedfile.pdf', 2 => 'file-to-path/thumbs/notallowedfile.psd', 3 => 'file-to-path/project/test/notallowedfile.txt' ))); ?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM