PHP code library

This is an extra subpage with various PHP codes, implemented as functions.

These functions were used to solve various problems in real web projects.
The solution for one problem may be implemented in many different ways (short, verbose, faster or nicer). We present own implementations, used in the past.


Function readCsvFile() - how to read content / values from CSV file in PHP:


The code
  /**
   * Read content from CSV file.
   * 
   * @param string $filename
   * @return string
   */
  public static function readCsvFile($filename)
  {
    $localPath = ''; // path to file
    $dataPath = $localPath . $filename;
    
    $separator = ";";
    
    $outArr = array();
    
    $h = fopen($dataPath, "r");
    while (($data = fgetcsv($h, 1024, $separator)) !== false)
    {
      $num = count($data);
      for ($c = 0; $c < $num; $c++)
      {
        $t = trim($data[$c]);
        if (strlen($t) == 0) continue;
        
        $outArr[] = $data[$c];
      }
    }
    fclose($h);
    
    return print_r($outArr, true);
  }

Choose from currently available implementations:


This code library will be gradually supplemented. Probably we will create code library also for other languages (such as JavaScript, Python, Ruby).
Would you get more codes? Or maybe suggest further functions? Please use contact page: Contact