根据禁用单词数组和禁用文件扩展名数组过滤文件名数组

发布于 2024-08-09 14:25:55 字数 504 浏览 1 评论 0原文

我正在使用 PHP,并且有一组需要过滤的用户图像。我需要执行 2 个不同的过滤器:

  1. 查看原始数组,看看每个值是否包含“坏词”数组中的值
  2. 查看原始数组中的值是否以“坏扩展”值之一结尾

图片数组:

Array  
(  
    [0] => smiles.gif  
    [1] => kittens.jpg  
    [2] => biscuits.png  
    [3] => butthead.jpg  
)  

$bad_words = array('beavis','butthead','winehouse');  
$bad_extensions = array('.gif','.tiff');  

I希望它返回:

Array  
(  
    [0] => kittens.jpg  
    [1] => biscuits.png  
)

I am using PHP and I have an array of user images that I need to filter. I need to do 2 different filters:

  1. Look in original array and see if each value contains a value in my "bad words" array
  2. Look and see if the value in the original array ends in one of "bad extensions" values

Images Array:

Array  
(  
    [0] => smiles.gif  
    [1] => kittens.jpg  
    [2] => biscuits.png  
    [3] => butthead.jpg  
)  

$bad_words = array('beavis','butthead','winehouse');  
$bad_extensions = array('.gif','.tiff');  

I would like it to return:

Array  
(  
    [0] => kittens.jpg  
    [1] => biscuits.png  
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜嗑 2024-08-16 14:25:55
$array = array("smiles.gif", "kittens.jpg", "biscuits.png", "butthead.jpg");

$new_arr = array_filter($array, "filter");

function filter($element) {
    $bad_words = array('beavis','butthead','winehouse');  
    $bad_extensions = array('gif','tiff');

    list($name, $extension) = explode(".", $element);
    if(in_array($name, $bad_words))
        return;

    if(in_array($extension, $bad_extensions))
        return;

    return $element;
}


echo "<pre>";
print_r($new_arr);
echo "</pre>";

输出

Array
(
    [1] => kittens.jpg
    [2] => biscuits.png
)

我删除了 .从您的扩展中

编辑:添加了邪恶的跳蚤修正

$array = array("smiles.gif", "kittens.jpg", "biscuits.png", "butthead.jpg");

$new_arr = array_filter($array, "filter");

function filter($element) {
    $bad_words = array('beavis','butthead','winehouse');  
    $bad_extensions = array('gif','tiff');

    list($name, $extension) = explode(".", $element);
    if(in_array($name, $bad_words))
        return;

    if(in_array($extension, $bad_extensions))
        return;

    return $element;
}


echo "<pre>";
print_r($new_arr);
echo "</pre>";

Outputs

Array
(
    [1] => kittens.jpg
    [2] => biscuits.png
)

I removed the . from your extensions tho

edit: added wicked fleas correction

似梦非梦 2024-08-16 14:25:55

首先,我会删除您的扩展列表中的句点。这只会让代码变得更加困难。如果你已经这样做了,下面的(未经测试的)代码应该可以工作,或者至少是一个开始

$cleanArray = [];

foreach($array as $value) {

    $extension = path_info($value, PATHINFO_EXTENSION);

    if (in_array($extension, $bad_extensions)) {
        continue;
    }

    foreach($bad_words as $word) {
        if (strstr($word, $value)) {
            continue 2;
        }
    }
    $cleanArray[] = $value;


}

$cleanArray 应该有你想要的值。

以下是 PHP 在线文档中的一些方便参考

Firstly, I'd remove the periods in your extension list. It will only make the code more difficult. If you've done this, the following (untested) code should work, or at least be a start

$cleanArray = [];

foreach($array as $value) {

    $extension = path_info($value, PATHINFO_EXTENSION);

    if (in_array($extension, $bad_extensions)) {
        continue;
    }

    foreach($bad_words as $word) {
        if (strstr($word, $value)) {
            continue 2;
        }
    }
    $cleanArray[] = $value;


}

$cleanArray should have the values you want.

Here are some handy references from the PHP online docs

喜你已久 2024-08-16 14:25:55

您可以在 PHP 中使用 array_filter 函数,只需编写一个函数进行你想要的过滤然后调用

array_filter($array1, "custom_filter");

You could use the array_filter function in PHP, just write a function that does the filtering you want then call

array_filter($array1, "custom_filter");
逆蝶 2024-08-16 14:25:55

因为不区分大小写的匹配是合理的包含,并且因为 in_array() 没有不区分大小写的模式,所以我可能只是从两个数组生成一个正则表达式模式,并让正则表达式引擎执行以下操作繁琐的过滤。

代码:(演示

$branches = implode(
    '|',
    array_merge(
        $bad_words,
        array_map(
            fn($v) => preg_quote($v, '/') . '

输出:

array (
  1 => 'kittens.jpg',
  2 => 'biscuits.png',
)
, $bad_extensions ) ) ); // beavis|butthead|winehouse|\.gif$|\.tiff$ var_export(preg_grep("/$branches/i", $array, PREG_GREP_INVERT));

输出:

Because case-insensitive matching is a reasonable inclusion and because in_array() does not have a case-insensitive mode, I'd probably just generate a regex pattern from the two arrays and let the regex engine do the tedious filtration.

Code: (Demo)

$branches = implode(
    '|',
    array_merge(
        $bad_words,
        array_map(
            fn($v) => preg_quote($v, '/') . '

Output:

array (
  1 => 'kittens.jpg',
  2 => 'biscuits.png',
)
, $bad_extensions ) ) ); // beavis|butthead|winehouse|\.gif$|\.tiff$ var_export(preg_grep("/$branches/i", $array, PREG_GREP_INVERT));

Output:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文