title: PHP - combine two TXT files with conditions
body: <p>(sorry in advance for the long question - the problem is actually simple - but to explain it is maybe not so simple )</p>
  
  <p>My noobie skills in PHP are challenged by this :</p>
  
  <p>Input of 2 TXT files with a structure like this :</p>
  
  <pre><code>$rowidentifier //number,letter,string etc..
  $some semi-fixed-string $somedelimiter $semi-fixed-string
  $content //with unknown length or strings or lines number.
  </code></pre>
  
  <p>reading the above , my meaning in "semi-fixed-string, means that it is a string with a KNOWN structure, but UNKNOWN content.. </p>
  
  <p>to give a practical example, let´s take an SRT file (i just use it as a guinea pig as the structure is very similar to what I need ):</p>
  
  <pre><code>1
  00:00:12,759 --&gt; 00:00:17,458
  "some content here "
  that continues here
  
  2
  00:00:18,298 --&gt; 00:00:20,926
  here we go again...
  
  3
  00:00:21,368 --&gt; 00:00:24,565
  ...and this can go forever...
  
  4
  .
  .
  .
  </code></pre>
  
  <p>what I want to do , is to take the the $content part from one file, and put it IN THE RIGHT PLACE at the second file .</p>
  
  <p>going back to the example SRT , having :</p>
  
  <pre><code>//file1 
  
      1
      00:00:12,759 --&gt; 00:00:17,458
      "this is the italian content "
      which continues in italian here
  
      2
      00:00:18,298 --&gt; 00:00:20,926
      here we go talking italian again ...
  </code></pre>
  
  <p>and </p>
  
  <pre><code>//file2 
  
      1
      00:00:12,756 --&gt; 00:00:17,433
      "this is the spanish, chinese, or any content "
      which continues in spanish, or chinese here
  
      2
      00:00:16,293 --&gt; 00:00:20,96
      here we go talking spanish, chinese or german again ...
  </code></pre>
  
  <p>will result in </p>
  
  <pre><code>//file3 
  
          1
          00:00:12,756 --&gt; 00:00:17,433
          "this is the italian content "
          which continues in italian here
          "this is the spanish, chinese, or any content "
          which continues in spanish, or chinese here
  
          2
          00:00:16,293 --&gt; 00:00:20,96
          here we go talking italian again ...
          here we go talking spanish, chinese or german again ...
  </code></pre>
  
  <p>or more php like :</p>
  
  <pre><code>$rowidentifier //unchanged
  $some semi-fixed-string $somedelimiter $semi-fixed-string //unchanged, except maybe an option to choose if to keep file1 or file2 ...
  $content //from file 1
  $content //from file 2
  </code></pre>
  
  <p>so, after all this introduction - this is what I have (which amounts to nothing actually..)</p>
  
  <pre><code>$first_file = file('file1.txt'); // no need to comment right ?
  $second_file = file('file2.txt'); // see above comment
  $result_array = array(); /construct array
  foreach($first_file as $key=&gt;$value) //loop array and.... 
  $result_array[]= trim($value).'/r'.trim($second_file[$key]); //..here is my problem ...
  
  // $Value is $content - but LINE BY LINE , and in our case, it could be 2-3- or even 4 lines
  // should i go by delimiters /n/r ??  (not a good idea - how can i know they are there ?? )
  // or should i go for regex to lookup for string patterns ? that is insane , no ?
  
  $fp = fopen('merge.txt', 'w+'); fwrite($fp, join("\r\n", $result_array); fclose($fp);
  </code></pre>
  
  <p>this will do line by line - which is not what i need. I need conditions..
  also - I am sure this is not a smart code, or that there are many better ways to go at it - so any help would be appreciated ... </p>