폴더에서 파일 이름을 가져 와서 날짜순으로 정렬 한 다음 파일에 대한 링크를 만드는 기능이 있습니다. 그러나 실제 파일 이름에 단어 사이에 공백이있는 경우에만 작동합니다. 파일 이름에 하이픈을 추가하면 날짜 순서가 올바르게 작동하지 않습니다. 작업이preg_match 코드를 수정하여 파일 이름을 날짜순으로 정렬 하시겠습니까?
파일 이름 :
프리젠 테이션 월 2011.pdf
프리젠 테이션 당당한 2010.pdf
프리젠 테이션 내가에 하이픈을 추가하는 경우
을 2010.pdf 수 주문이 파기되는 파일 이름 :
프리젠 테이션 월-2011.pdf 있도록
가 발표 8 월-2010.pdf이
이프리젠 테이션 월-2010.pdf 내가는 preg_match을 (변경할 수있는 방법
)이 걸리는 계정 하이픈에? 여기 내 코드는 다음과 같습니다.
$linkdir="documents/presentations";
$dir=opendir("documents/presentations");
$files=array();
while (($file=readdir($dir)) !== false)
{
if ($file != "." and $file != ".." and $file != "index.php")
{
array_push($files, $file);
}
}
closedir($dir);
function date_sort_desc($a, $b)
{
preg_match('/\w+ \d{4}/', $a, $matches_a);
preg_match('/\w+ \d{4}/', $b, $matches_b);
$timestamp_a = strtotime($matches_a[0]);
$timestamp_b = strtotime($matches_b[0]);
if ($timestamp_a == $timestamp_b) return 0;
return $timestamp_a < $timestamp_b;
}
usort($files, 'date_sort_desc');
foreach ($files as $file){
$name = substr($file, 0, strrpos($file, '.'));
$filename = str_replace(" ", "%20", $file);
$name = str_replace("-", " ", $file);
print "<li><a href='/$linkdir/$filename' rel='external'>$name</a></li>";
}
이 점에 대해 도움을 주시면 감사하겠습니다.
슈퍼 스타 - 정말 고마워요! – Jonathan