txt 파일의 각 줄을 새 배열 요소로 읽습니다.
텍스트 파일의 모든 줄을 배열로 읽고 각 줄을 새 요소에 넣으려고합니다.
지금까지 내 코드.
<?php
$file = fopen("members.txt", "r");
$i = 0;
while (!feof($file)) {
$line_of_text = fgets($file);
$members = explode('\n', $line_of_text);
fclose($file);
?>
당신은 특별한 처리를 필요로하지 않는 경우, 이것은 당신이 찾고있는 무엇을해야합니까
$lines = file($filename, FILE_IGNORE_NEW_LINES);
내가 찾은 가장 빠른 방법은 다음과 같습니다.
// Open the file
$fp = @fopen($filename, 'r');
// Add each line to an array
if ($fp) {
$array = explode("\n", fread($fp, filesize($filename)));
}
여기서 $ filename은 파일의 경로 및 이름입니다. ../filename.txt.
텍스트 파일을 설정 한 방법에 따라 \ n 비트를 가지고 놀아야 할 수도 있습니다.
이것을 사용하십시오 :
$array = explode("\n", file_get_contents('file.txt'));
<?php
$file = fopen("members.txt", "r");
$members = array();
while (!feof($file)) {
$members[] = fgets($file);
}
fclose($file);
var_dump($members);
?>
$yourArray = file("pathToFile.txt", FILE_IGNORE_NEW_LINES);
FILE_IGNORE_NEW_LINES
각 배열 요소의 끝에 개행을 추가하지 마십시오. 빈 줄을 건너 뛰는 데
사용할 수도 있습니다.FILE_SKIP_EMPTY_LINES
여기 참조
$lines = array();
while (($line = fgets($file)) !== false)
array_push($lines, $line);
분명히 먼저 파일 핸들을 만들고 $file
.
다음과 같이 간단합니다.
$lines = explode("\n", file_get_contents('foo.txt'));
file_get_contents()
-전체 파일을 문자열로 가져옵니다.
explode("\n")
-구분 기호로 문자열을 분할합니다 "\n"
.-개행 문자에 대한 ASCII-LF 이스케이프는 무엇입니까?
그러나주의를 기울이십시오. 파일에 UNIX 행 끝 이 있는지 확인하십시오 .
경우 "\n"
제대로 작동하지 않습니다 당신은 다른 줄 바꿈 OF2 코딩이 당신은 시도 할 수 있습니다 "\r\n"
, "\r"
또는"\025"
$file = __DIR__."/file1.txt";
$f = fopen($file, "r");
$array1 = array();
while ( $line = fgets($f, 1000) )
{
$nl = mb_strtolower($line,'UTF-8');
$array1[] = $nl;
}
print_r($array);
올바른 길을 가고 있었지만 게시 한 코드에 몇 가지 문제가있었습니다. 우선 while 루프에는 닫는 대괄호가 없습니다. 둘째, $ line_of_text는 모든 루프 반복으로 덮어 쓰여지며, 이는 루프에서 =를. =로 변경하여 수정됩니다. 셋째, 실제 개행 문자가 아니라 리터럴 문자 '\ n'을 분해합니다. PHP에서 작은 따옴표는 리터럴 문자를 나타내지 만 큰 따옴표는 실제로 이스케이프 된 문자와 변수를 해석합니다.
<?php
$file = fopen("members.txt", "r");
$i = 0;
while (!feof($file)) {
$line_of_text .= fgets($file);
}
$members = explode("\n", $line_of_text);
fclose($file);
print_r($members);
?>
$file = file("links.txt");
print_r($file);
This will be accept the txt file as array. So write anything to the links.txt file (use one line for one element) after, run this page :) your array will be $file
This has been covered here quite well, but if you REALLY need even better performance than anything listed here, you can use this approach that uses strtok
.
$Names_Keys = [];
$Name = strtok(file_get_contents($file), "\n");
while ($Name !== false) {
$Names_Keys[$Name] = 0;
$Name = strtok("\n");
}
Note, this assumes your file is saved with \n
as the newline character (you can update that as need be), and it also stores the words/names/lines as the array keys instead of the values, so that you can use it as a lookup table, allowing the use of isset
(much, much faster), instead of in_array
.
참고URL : https://stackoverflow.com/questions/6159683/read-each-line-of-txt-file-to-new-array-element
'Programming' 카테고리의 다른 글
scrollview에 중력이 없습니다. (0) | 2020.08.19 |
---|---|
Android 알림이 표시되지 않음 (0) | 2020.08.19 |
UIStackView 내부에 추가 된 뷰에 선행 패딩을 추가하는 방법 (0) | 2020.08.19 |
grunt : 터미널에서 실행할 때 명령을 찾을 수 없음 (0) | 2020.08.19 |
Maven은 왜 그렇게 나쁜 담당자를 가지고 있습니까? (0) | 2020.08.19 |