preg_replace # 폴더 인식문제

preg_replace # 폴더 인식문제

QA

preg_replace # 폴더 인식문제

답변 2

본문

안녕하세요.

특정 디렉토리에 대괄호가 들어가면

$img_path_dir  = "/volume/[0001] 이모티콘";

$files = glob($img_path_dir.'/*.*');

 

인식이 안되어 폴더 안에 파일 리스트가 보여지지 않아

아래 처럼하면 폴더가 인식되어 파일 리스트가 보여집니다.

$img_path_dir       = "/volume/[0001] 이모티콘";

$img_path_dir       = preg_replace('~[\[?{]~','[$0]',$img_path_dir); // 대괄호 인식문제 해결

$files                   = glob($img_path_dir.'/*.*');

 

그런데 # 들어간 폴더는 어떻게 해야 되는지 모르겠네요

$img_path_dir       = "/volume/최종화 #나는";

$img_path_dir       = preg_replace('~[\[?{]~','[$0]',$img_path_dir); // # 인식 시키기

$files                   = glob($img_path_dir.'/*.*');

natsort($files); // 보기 좋게 정렬 
foreach ( $files as $file ) // 반복 
{
        echo "<img src='{$file}'><br>";
}

감사합니다.

이 질문에 댓글 쓰기 :

답변 2

$img_path_dir       = preg_replace('~[\[?{]~','[$0]',$img_path_dir); // # 인식 시키기

요디렉토리경로  문제 있는듯 ~이게뭐죠? 그리고 {  ]  이건 ;;;;---;;;

$img_path_dir = preg_replace('/([#\[\]{}?])/', '\\\\$1', $img_path_dir);

이렇게 해보시구 않되면

$img_path_dir = "/volume/최종화 #나는";
$img_path_dir = preg_replace('/([\[?{}#])/', '[$1]', $img_path_dir); // 대괄호 및 # 인식 문제 해결
$files = glob($img_path_dir.'/*.*');
natsort($files); // 보기 좋게 정렬

foreach ($files as $file) {
    echo "<img src='{$file}'><br>";
}

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
filter #php ×
전체 15,331
© SIRSOFT
현재 페이지 제일 처음으로