使用通配符从php数组中删除特定键

时间:2014-02-27 15:48:04

标签: php arrays

我正在尝试在数组中搜索特定的部分字符串,并删除包含它们的所有键。我已经尝试了几件事,我的PHP-fu已经用完了......

我正在搜索的数组(部分但很多沿着这些行)是这样的:

Array
(
    [0] => /.
    [1] => /..
    [2] => /htdocs
    [3] => /htdocs/.
    [4] => /htdocs/..
    [5] => /htdocs/zipper_new.php
    [6] => /htdocs/bottom.gif
)

我正在尝试删除包含“/”的所有条目。和“/ ..”。

它的长短是我制作了一个脚本来按文件类型搜索文件并对它们进行tar,我正在尝试添加功能而不是搜索所有文件并执行相同的操作。这部分内容未能引用条目包含这些字符串的全部或部分内容。

2 个答案:

答案 0 :(得分:1)

$data = array(
    '/.',
    '/..',
    '/htdocs',
    '/htdocs/.',
    '/htdocs/..',
    '/htdocs/zipper_new.php',
    '/htdocs/bottom.gif',
);

$match = '/.';

$newData = array_filter(
    $data,
    function($value) use ($match) {
        return (!fnmatch('*' . $match . '*', $value));
    }
);

var_dump($newData);

答案 1 :(得分:0)

您不能简单地使用foreach循环并使用strstr之类的函数查找字符串吗?

然后,使用unset删除条目。

foreach ($table as $key => $item) {
    if (strstr($item, '/.')) // If it contains /. it will also match for /..
        unset($table[$key]);
}
相关问题