title: Create new txt files from each line of a txt file
body: <p>I have a various txt files like ex: dictionary , each new line is term and then description. pipe delimited.</p>
  
  <p>I have wrote simple script that actually reads each line and then creates (fwrite) new txt file named after term and description as a text of that file.</p>
  
  <p>It works but I'm wondering if there is a better approach, one that takes special characters into account, buffer perhaps, Not sure where to start.</p>
  
  <pre><code>$file = fopen("test.txt", "r") or exit("Unable to open file!");
  //  Output a line of the file until the end is reached
  while(!feof($file))
  {
      $line = fgets($file);
  
      // making all lowercase - optional
      $line = strtolower($line);
  
      // take the first value before delimiter
      $var = substr( $line, 0, strpos( $line, '|' ) );
  
      // remove some characters - optional ( depends on a file structure and contents )
      $var = str_replace("-", "", $var);
  
      // what txt should be written into a each new file
      $txt = str_replace("|", "", $line);
  
      // name the file 
      $myfile = fopen("$var.txt", "w") or die("Unable to open file!");
  
      // write
      fwrite($myfile, $txt);
  
      //close each
      fclose($myfile);
  
  }
  
  //close
  fclose($file);
  </code></pre>
  
  <p>UPDATE</p>
  
  <pre><code>$file = fopen("test.txt", "r") or exit("Unable to open file!");
  //  Output a line of the file until the end is reached
  while(!feof($file))
      {
          $line = fgets($file);
  
          // @sorak fix
          $line = fgets($file);
          $name = preg_replace('/[^0-9a-zA-Z]/', '', 
            explode('|', $line)[0]);
  
          // making it lowercase - optional for each
          $line = strtolower($line);
          $name = strtolower($name);
  
          // what txt should be written into a each new file
          $txt = str_replace("|", " ", $line);
  
          // name the file 
          $myfile = fopen("$name.txt", "w") or die("Unable to open file!");
  
          // write
          fwrite($myfile, $txt);
  
          // action echo
          echo "$myfile - $name - $txt &lt;/br&gt;";
  
          //close each
          fclose($myfile);
  
      }
  
  //close
  fclose($file);
  </code></pre>
  
  <p>Since last update contained some duplicate code lines/bugs that caused skipping every other line :) I'm posting new fixed and bit upgraded version. </p>
  
  <pre><code>// this utility is for creating multiple names.txt files from separate lines in original.txt file
  // format for original file is: is name|text
  
  // increase memory limit to 32M
  ini_set('memory_limit','32M');
  
  // increase 1440 seconds = 24 minutes
  ini_set('max_execution_time', 1440);
  
  $file = fopen("original.txt", "r");
  if ($file) {
      while (($line = fgets($file)) !== false) {
  
          // @sorak fix
          //$line = fgets($file);
          $name = preg_replace('/[^0-9a-zA-Z]/', '', 
            explode('|', $line)[0]);
  
          // making it lowercase - optional for each
          $line = strtolower($line);
          $name = strtolower($name);
          $filename = $name . ".txt";
  
          // what text should be written into a each new file
          // change pipe separator if needed
          $txt = str_replace("|", " ", $line);
  
          // set values 
          $myfile = fopen($filename, "a") or die("Unable to open file!");
          $dir = "/example.com/could_be_dynamic_folder_name";
  
          // write
          fwrite($myfile, $txt);
          // chmod($dir,0777); optional if creation of dir is from GET/other value and not in the same parent
  
          // action echo
          echo "$myfile &lt;/br&gt;";
          echo "$dir/$filename &lt;/br&gt;";
          echo "$txt &lt;/br&gt;&lt;/br&gt;";
  
          // chmod
          chmod($dir,0777);
          chmod("$dir/$filename",0666); // remember to set this script to 0666 
  
      }
  
      fclose($file);
  
  } else {
  
      // error echo
      echo "something went wrong, error";
  
  } 
  </code></pre>
  
  <p>Works like a charm now. Case closed.</p>