어떤 종류의 의미가 사실은 당신이 이전의 내용을 덮어 쓰는 동안 그 파일에 항상 현재의 URL을 쓰고 있다는 사실입니다. foreach-loop의 모든 단계에서 해당 파일을 다시 열고 내용을 지우고 해당 파일에 URL 하나를 작성합니다. 다음 단계에서는 정확히 동일한 파일을 다시 열고 다시 실행합니다. 그래서 그 파일의 마지막 URL로 끝나는 것입니다.
는
당신은 디스크에 독특한 사람을 쓰기 한 후, 배열에있는 모든 URL을 수집 중복을 버리고해야합니다 : (훨씬 더 이전의 방법에서 영감)
$lines = file($fopen2);
$urls = array(); // <-- create empty array for the urls
foreach ($lines as $line) {
$rfi_links = explode('=', $line, 2); // <-- you need only two parts, rights?
$urls[] = $rfi_links[0]; // <-- push new URL to the array
}
// Remove duplicates from the array
$links_duplicate_removed = array_unique($urls);
// Write unique urls to the file:
file_put_contents($rfi.$file.$ext, implode(PHP_EOL, $links_duplicate_removed));
또 다른 해결책은 라인 반복을 시작하기 전에 한 번 파일을 엽니 다 :
$lines = file($fopen2);
$urls = array();
// Open file
$fp = fopen($rfi.$file.$ext, 'w');
foreach ($lines as $line) {
$rfi_url = explode('=', $line, 2);
// check if that url is new
if (!in_array($rfi_url[0], $urls)) {
// it is new, so add it to the array (=mark it as "already occured")
$urls[] = $rfi_url[0];
// Write new url to the file
fputs($fp, $rfi_url[0] . PHP_EOL);
}
}
// Close the file
fclose($fp);
foreach 루프에서 파일을 열고 매번 파일을 자르는 것 외에도 foreach 루프 및 파일이 기록 될 때까지 중복 파일을 제거하지 않으므로 중복 파일을 포함한 모든 내용이 파일에 저장됩니다. – Jason