A class used to read textfiles


/**
 * TextProcessor.
 * 
 * @author Dribbit info@dribbit.eu
 *
 */
class TextProcessor {

  /**
   * Process textfiles and return as an associated array.
   * 
   * @param $file
   *  String File path.
   * @param $length
   * Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), 
   * or on EOF (whichever comes first). If no length is specified, 
   * it will keep reading from the stream until it reaches the end of the line. 
   *  
   * @return array
   * 
   */
  public static function Process ($file, $length = 0) {
    
    $rows = array();
    $handle = fopen($file, 'r');
    
    if (!$handle)
      exit('Unable to open file.');
    
    if($length == 1)
      exit('Length can\'t be 1.');
    
    while (($buffer = fgets($handle, $length)) !== false) {
      $output[] = ($buffer);
    } 

    fclose($handle);
    
    return $output;
  }
  
}

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.