如何使用模式搜索文件 [英] How to search file with pattern
问题描述
我的文件夹中有很多文件,并且我需要对某些文件进行删除这些文件具有类似
的格式 messages.bm.inc.phpmessages.cn.inc.phpmessages.en.inc.php
这些文件是动态创建的,但是模式在那里
在此之前,我通常使用以下代码删除我的文件,然后重复执行
$ filename ="messages.en.inc.php";如果(file_exists($ filename)){unlink($ filename);}
现在我的处境更加动态,我需要使用模式搜索这些文件并将其删除,请提出一种解决方法,谢谢
$ files = glob("path_to_your_files/messages.*.inc.php");array_map('unlink',$ files);
通过 glob
,您将以指定的模式从文件夹中获取所有文件, array_map
将为匹配的文件数组实现 unlink
功能./p>
I have a folder with many file , and i need to perform a deletion with certain file and those file have a pattern like
messages.bm.inc.php
messages.cn.inc.php
messages.en.inc.php
Those file are dynamically created , but the pattern is there
Before this i normally delete my file with below code , and repeat it
$filename="messages.en.inc.php";
if (file_exists($filename)) {
unlink($filename);
}
Now i having a more dynamic situation , i need search through those file with the patern and delete it , please suggest a way to do , thanks
$files = glob("path_to_your_files/messages.*.inc.php ");
array_map('unlink', $files);
By glob
you will get all your files from folder by specified pattern, array_map
will implement unlink
function for array of matched files.
这篇关于如何使用模式搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!