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 generateRandomString() - how to generate random string with length specified:


The code
  /**
   * Generate random string, with letters and digits combined.
   *
   * @param integer $length A length of generated output
   * @param boolean $with_ext_set If extended set of chars should be included
   * 
   * @return string
   */
  public static function generateRandomString($length = 32, $with_ext_set = false)
  {
    list($usec, $sec) = explode(' ', microtime());
    $s_in = (float) $sec + ((float) $usec * 100000);
    srand($s_in);

    $input = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if ($with_ext_set)
    {
      $input .= "!@#$%^&*()_+-=[]{}|;:<>,./?";
    }
    
    $output = "";
    while (strlen($output) < $length)
    {
      $output .= substr($input, rand() % (strlen($input)), 1);
    }

    return $output;
  }

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