title: PHP: Insert line break after every $num word in a text file
body: <p>I want to clean up a .txt file to make it more readable by inserting a line break after every 4th word. The file has been filled with data from an array.</p>
  
  <p>This is what I've come up with so far by looking at <a href="http://www.webdeveloper.com/forum/showthread.php?265789-How-to-add-a-linebreak-in-a-string-every-two-words" rel="nofollow noreferrer">this</a> and <a href="https://stackoverflow.com/questions/21238127/split-text-file-for-every-x-lines">this</a>:</p>
  
  <pre><code>&lt;?php
  require "simple_html_dom.php";
  
  $html = file_get_html("http://www.lipsum.com/");
  
  $data = array();
  $counter = 0;
  
  
  foreach($html-&gt;find("div") as $tr){
      $row = array();
      foreach($tr-&gt;find("div") as $td){
          $row[] = $td-&gt;plaintext;
      }
       $data[] = $row;
  
      }
  
  
  ob_start();
  var_dump($data);
  
  $data = preg_replace('~((\w+\s){4})~', '$1' . "\n", $data);
  file_put_contents('new_text_file.txt', $data);
  
  
  //$handlefile = fopen("newfile.txt", "w") or die("Unable to open file!");
  //file_put_contents('newfile.txt', $data);
  //$output = ob_get_clean();
  //$outputFile = "newfile.txt";
  
  
  //fwrite($handlefile, $output);
  //fclose($handlefile);
  
  
  
  ?&gt;
  </code></pre>
  
  <p>As you can see I've created a for loop and two if statements to "count" the spacing between the words, and when the <code>counter</code> variable has reached 3, a line break is inserted. But it doesn't work since no data from the website is printed to the text file in the first place. It works however, if I remove the for loop and the if statements, without the sorting of course.
  Any type of help is appriciated!</p>
  
  <p>Edit: updated code.</p>
  
  <p>Edit 2: final working version. Original question left for further reference.</p>
  
  <p>This is the final working version:</p>
  
  <pre><code>&lt;?php
  require "simple_html_dom.php";
  
  $html = file_get_html("http://www.lipsum.com/");
  $data = array();
  $counter = 0;
  
  
  foreach($html-&gt;find("div") as $tr){
      $row = array();
      foreach($tr-&gt;find("div") as $td){
          $row[] = $td-&gt;plaintext;
      }
       $data[] = $row;
  
      }
  
  
  ob_start();
  var_dump($data);
  $handlefile = fopen("newfile.txt", "w") or die("Unable to open file!");
  file_put_contents('newfile.txt', $data);
  $output = ob_get_clean();
  $outputFile = "newfile.txt";
  fwrite($handlefile, $output);
  fclose($handlefile);
  
  function MakeFileReadable($source , $export) {
  
      $content = file_get_contents($source);
      $x = explode (" " , $content);
  
      $newx = "";
      $count = 1;
      foreach ($x as $word) {
          $newx .= $word . " ";
          if ($count %12 ==0) $newx .= "\r\n";
          $count ++;
      }
  
      $fp = fopen("file-export.txt" , 'w');
      if (!$fp) die("There is a problem with opening file...");
      fwrite($fp , $newx);
      fclose($fp);
  
  }
  
  MakeFileReadable("newfile.txt" , "file-export.txt");
  
  ?&gt;
  </code></pre>