본문 바로가기
개발/php

php file('abc.txt'); text파일을 라인별 배열로 읽어오는 기능 예제

by 향유 2021. 2. 23.

 

필요 파일) data.txt 

hello world
good morning

 

실행소스)

_DIR = 'public_html/data';

$f = file(_DIR.'/data.txt');

 

결과 화면)

var_dump( $f ); # array(2) {

# [0]=> # string(12) "hello world # "

# [1]=> # string(13) "good morning # " # }

 

응용) 배열을 다시 라인으로 변경

if ($f != null){
   for($i = 0;$i < count($f);$i++){
       $arrData['list'] .= $f[$i];
    }
 }

 

댓글