Example #1
0
 /**
  * Strips whitespace (or other UTF-8 characters) from the end of a string.
  * @see http://php.net/rtrim
  *
  * @author  Andreas Gohr <*****@*****.**>
  *
  * @param   string   input string
  * @param   string   string of characters to remove
  * @return  string
  */
 public static function rtrim($str, $charlist = NULL)
 {
     if ($charlist === NULL) {
         return rtrim($str);
     }
     if (str::is_ascii($charlist)) {
         return rtrim($str, $charlist);
     }
     $charlist = preg_replace('#[-\\[\\]:\\\\^/]#', '\\\\$0', $charlist);
     return preg_replace('/[' . $charlist . ']++$/uD', '', $str);
 }
Example #2
0
 /**
  * Generates a random string of a given type and length.
  *
  * @param   string   a type of pool, or a string of characters to use as the pool
  * @param   integer  length of string to return
  * @return  string
  *
  * @tutorial  alnum    - alpha-numeric characters
  * @tutorial  alpha    - alphabetical characters
  * @tutorial  numeric  - digit characters, 0-9
  * @tutorial  nozero   - digit characters, 1-9
  * @tutorial  distinct - clearly distinct alpha-numeric characters
  */
 public static function random($type = 'alnum', $length = 8)
 {
     $utf8 = NO;
     switch ($type) {
         case 'alnum':
             $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
             break;
         case 'alpha':
             $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
             break;
         case 'numeric':
             $pool = '0123456789';
             break;
         case 'nozero':
             $pool = '123456789';
             break;
         case 'distinct':
             $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
             break;
         default:
             $pool = (string) $type;
             $utf8 = !str::is_ascii($pool);
             break;
     }
     $str = '';
     $pool_size = $utf8 === YES ? mb_strlen($pool) : strlen($pool);
     for ($i = 0; $i < $length; $i++) {
         $str .= $utf8 === YES ? mb_substr($pool, mt_rand(0, $pool_size - 1), 1) : substr($pool, mt_rand(0, $pool_size - 1), 1);
     }
     return $str;
 }
Example #3
0
 /**
  * Recursively cleans arrays, objects, and strings. Removes ASCII control
  * codes and converts to UTF-8 while silently discarding incompatible
  * UTF-8 characters.
  *
  * @param   string  string to clean
  * @return  string
  */
 public static function clean($str)
 {
     if (is_array($str) or is_object($str)) {
         foreach ($str as $key => $val) {
             // Recursion!
             $str[Input::clean($key)] = Input::clean($val);
         }
     } elseif (is_string($str) and $str !== '') {
         // Remove control characters
         $str = str::strip_ascii_ctrl($str);
         if (!str::is_ascii($str)) {
             // Disable notices
             $ER = error_reporting(~E_NOTICE);
             // iconv is expensive, so it is only used when needed
             $str = iconv(Eight::CHARSET, Eight::CHARSET . '//IGNORE', $str);
             // Turn notices back on
             error_reporting($ER);
         }
     }
     return $str;
 }