PHP PHP에서 파일 확장자 추출하는 5가지 방법
페이지 정보
조회 167회
작성일 20-04-28 16:34
PHP에서 파일명으로부터 확장자를 얻는 5가지 방법을 예제와 함께 비교 정리했다. strrchr 함수로 마지막 점 이후를 잘라내는 방법, strrpos와 substr 조합, explode와 end 조합, preg_replace 정규식, pathinfo 함수를 각각 사용하는 코드를 소개하고, 상황에 맞는 방법을 선택해 활용할 수 있다.
$filename = "mypage.asp";
//1 使用strrchr函数求得
$ext = substr(strrchr($filename, '.'), 1);
echo$ext;
//2 使用strrpos和substr
$ext = substr($filename, strrpos($filename, '.') + 1);
echo$ext;
//3 使用expload 和 end
$ext = end(explode('.', $filename));
echo$ext;
//4 使用 preg_replace 带正则表达式
$ext = preg_replace('/^.*\.([^.]+)$/D', '$1', $filename);
echo$ext;
//5 使用pathinfo
$fileinfo = pathinfo($filename);
$ext = $fileinfo['extension'];
echo$ext; - 이전글jQuery로 Select(콤보박스) 값 가져오기·설정·옵션 추가/삭제 20.04.28
- 다음글jQuery 이미지 확대 플러그인 ckshow_Img 1.6 사용법 20.04.28