我需要在符合特定條件的目錄中找到文件。例如,我知道文件名以'123-'開頭,以.txt結尾,但我不知道兩者之間的內容。使用preg_match在目錄中查找文件?
我已經啓動了獲取目錄中的文件和preg_match的代碼,但是卡住了。如何更新以找到我需要的文件?
$id = 123;
// create a handler for the directory
$handler = opendir(DOCUMENTS_DIRECTORY);
// open directory and walk through the filenames
while ($file = readdir($handler)) {
// if file isn't this directory or its parent, add it to the results
if ($file !== "." && $file !== "..") {
preg_match("/^".preg_quote($id, '/')."\\-(.+)\\.txt$/" , $file, $name);
// $name = the file I want
}
}
// tidy up: close the handler
closedir($handler);
使用這個:'/ ^(123 - 。*。txt)/ i';匹配文件名稱以123開頭 - 任何內容以.txt結尾。 – phpisuber01