변수의 한 줄은 좋지만 텍스트 파일의 여러 줄을 변수에 할당하는 방법은 무엇입니까?
-샘플 텍스트 파일 ----
jeff ->line0
35 ->line1
123456 ->line2
"this is ->line3
the message ->line4
from you" ->line5
3,4 및 5 행을 단일 변수에 할당하는 방법은 무엇입니까?
암호:
<?php
$lines = file('sample.txt');
$vara= $lines[0];
$varb= $lines[1];
$varc= $lines[2];
?>
다음과 같이 할 수 있습니다.
//concatenate the last 3 lines and remove the break lines
$result=preg_replace('/\s{2,}/', ' ', $lines[3].$lines[4].$lines[5]);
print $result;
"this is the message from you"
파일의 모든 줄을 반복합니다.
$lines = file('sample.txt');
$result="";
if (array_key_exists(3,$lines)) {
//assuming that always the comment begins from line 3
for($i=3; $i<count($lines);$i++){
$result .=preg_replace('/\s{2,}/', ' ', $lines[$i]);
}
}
print $result;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다