2014-02-08 1 views
0

루프 어디서도이에 시작하는 완전히 의아해 해요,하지만 난 목록 B에 이들과 함께PHP와 함께 두 개의 텍스트 파일을 결합 - foreach는이

을 같은 파일 A에 키워드 목록을 제공 할 필요가

파일 A : :
1 호선
2 호선
세 번째 줄

너무 파일은 내가 예를 들어 파일 B

에서 foreach는 라인의 라인을 추가 할

파일 B :
TEST1
TEST2
TEST3

combined.txt 파일로 출력 :

line1test1
line2test1
line3test1
line1test2 ... 그래서

에 연구 할 스크립트의 일부, 샘플 스크립트 또는 심지어 wor를 제공 할 수 있다면 그것을하는 왕 방법. 나는 그것을 매우 감사 할 것이다. 이 도움이

<?php 

$file1 = 'keywords.txt'; 
$file2 = 'topics.txt'; 
$combined = 'combined.txt'; 

$keywords = fopen("keywords.txt", "rb"); 
$topics = fopen("topics.txt", "rb"); 
$front = explode($topics); 
$back = explode($topics); 

while (!feof($keywords)) { 

file_put_contents($combined, . $front ."". $back . "\n"); 

fclose($keywords & $topics); 
} 
?> 
+1

적어도 시도한 것을 증명하는 코드 샘플을 제공해야합니다. – jeremyjjbrown

답변

1

희망 : 요청에 따라

, 여기 내 샘플 코드입니다. 코드 전체에 주석을 뿌려서 내가하는 일에 대한 충분한 설명이되기를 바랍니다.

  • fopen
  • trim
  • fgets
  • fwrite
  • fclose
  • : 여기
    <?php 
    
    // Open keywords file for reading 
    $keywords_file = 'keywords.txt'; 
    $keywords_fh = fopen($keywords_file, 'r'); 
    
    // Get line by line from keywords file, push into $keywords array 
    // Make sure to trim each line from fgets, to strip off \n at end. 
    $keywords = array(); 
    while ($line = trim(fgets($keywords_fh))) { 
        array_push($keywords, $line); 
    } 
    fclose($keywords_fh); 
    
    // Open topics file for reading 
    $topics_file = 'topics.txt'; 
    $topics_fh = fopen($topics_file, 'r'); 
    
    // Get line by line from topics file, push into $topics array 
    // Make sure to trim each line from fgets, to strip off \n at end. 
    $topics = array(); 
    while ($line = trim(fgets($topics_fh))) { 
        array_push($topics, $line); 
    } 
    fclose($topics_fh); 
    
    // Open combined file for writing 
    $combined_file = 'combined.txt'; 
    $combined_fh = fopen($combined_file, 'w'); 
    
    // Iterate through each keyword. 
    // For each iteration, iterate through each topic. 
    // Write the concatenation of keyword and topic to file. 
    foreach ($keywords as $keyword) { 
        foreach ($topics as $topic) { 
        fwrite($combined_fh, "$keyword$topic\n"); 
        } 
    } 
    
    fclose($combined_fh); 
    

    내가 사용하는 주요 기능 중 일부 PHP 문서에 대한 몇 가지 링크입니다
+0

에 바닥재 이외의 여러 단어를 가지고 있지만 을 chandlerflooring 지붕 . 이것은 내가 원하는 것입니다. 링크 주셔서 감사합니다, 할 공부가있어! – CodingNoob

+0

즐거운 프로그래밍과 행복한 공부! –

1
$f1 = explode("\n",file_get_contents("fileA.txt")); 
$f2 = explode("\n",file_get_contents("fileB.txt")); 
foreach ($f1 as $key => $value) { 
    $f3[] = $value.$f2[$key]; 
} 
file_put_contents("fileC.txt", implode("\n",$f3)); 
+0

이이 출력 끝나는 것입니다 : 피닉스 배관 메사 난방 길버트 내가 대단히 감사합니다 fileB – CodingNoob

관련 문제