根据禁用单词数组和禁用文件扩展名数组过滤文件名数组
我正在使用 PHP,并且有一组需要过滤的用户图像。我需要执行 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:
- Look in original array and see if each value contains a value in my "bad words" array
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
输出
我删除了 .从您的扩展中
编辑:添加了邪恶的跳蚤修正
Outputs
I removed the . from your extensions tho
edit: added wicked fleas correction
首先,我会删除您的扩展列表中的句点。这只会让代码变得更加困难。如果你已经这样做了,下面的(未经测试的)代码应该可以工作,或者至少是一个开始
$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 should have the values you want.
Here are some handy references from the PHP online docs
您可以在 PHP 中使用 array_filter 函数,只需编写一个函数进行你想要的过滤然后调用
You could use the array_filter function in PHP, just write a function that does the filtering you want then call
因为不区分大小写的匹配是合理的包含,并且因为
in_array()
没有不区分大小写的模式,所以我可能只是从两个数组生成一个正则表达式模式,并让正则表达式引擎执行以下操作繁琐的过滤。代码:(演示)
输出:
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)
Output: