와 파일에서 읽어 : 나는 "값"외부 스크립트에서 그 변화를 분석 할 필요가PHP는 내가이 같은 형식 TXT 파일이 공간
string1 value
string2 value
string3 value
을하지만, stringX 정적이다. 각 줄마다 값을 얻으려면 어떻게해야합니까?
와 파일에서 읽어 : 나는 "값"외부 스크립트에서 그 변화를 분석 할 필요가PHP는 내가이 같은 형식 TXT 파일이 공간
string1 value
string2 value
string3 value
을하지만, stringX 정적이다. 각 줄마다 값을 얻으려면 어떻게해야합니까?
그래야 효과가 있습니다.
$lines = file($filename);
$values = array();
foreach ($lines as $line) {
if (preg_match('/^string(\d+) ([A-Za-z]+)$/', $line, $matches)) {
$values[$matches[1]] = $matches[2];
}
}
print_r($values);
이것은 도움이 될 수 있습니다. 한 번에 한 줄씩 읽습니다. 심지어 Text.txt에 1000 줄이 포함되어 있어도 file_put-contents("result.txt", $line[1])
과 같이 file_put_contents
과 같이 입력하면 줄 (또는 원하는 모든 작업이 실행될 때마다 파일이 업데이트됩니다.) ( 이후)은 1000 줄을 모두 읽지 않습니다. 그리고 언제든지 한 줄만 기억합니다.
<?php
$fp = fopen("Text.txt", "r") or die("Couldn't open File");
while (!feof($fp)) { //Continue loading strings till the end of file
$line = fgets($fp, 1024); // Load one complete line
$line = explode(" ", $line);
// $line[0] equals to "stringX"
// $line[1] equals to "value"
// do something with $line[0] and/or $line[1]
// anything you do here will be executed immediately
// and will not wait for the Text.txt to end.
} //while loop ENDS
?>
직접 해보았습니까? – Stony
묻기도 전에 Google에 대한 조사를 수행합니다. –
공간에 문제가 있습니다. 어떻게 처리 할 수 있는지 모르겠습니다. – user840718