특정 조건과 일치하는 디렉토리에서 파일을 찾아야합니다. 예를 들어 파일 이름이 '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'; 파일 이름과 일치합니다. 중간에 .125로 시작하고 .txt로 끝납니다. – phpisuber01