From 9ad6f36f3cb83b56535775e8c624de90d319e8eb Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 11 Jun 2011 20:12:13 -0400 Subject: [PATCH] Add functions.php, copied from phalanx --- base/functions.php | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 base/functions.php diff --git a/base/functions.php b/base/functions.php new file mode 100644 index 0000000..7e2b407 --- /dev/null +++ b/base/functions.php @@ -0,0 +1,76 @@ +. + +namespace hoplite\base; + +/*! + Iterates over an array and unsets any empty elements in the array. This + operates on the parameter itself. +*/ +function ArrayStripEmpty(Array & $array) +{ + foreach ($array as $key => $value) + if (is_array($array[$key])) + ArrayStripEmpty($array[$key]); + else if (empty($value)) + unset($array[$key]); +} + +/*! + Turns an under_scored string into a CamelCased one. If |$first_char| is + TRUE, then the first character will also be capatalized. +*/ +function UnderscoreToCamelCase($string, $first_char = TRUE) +{ + if ($first_char) + $string[0] = strtoupper($string[0]); + return preg_replace_callback('/_([a-z])/', + function($c) { return strtoupper($c[1]); }, + $string); +} + +/*! + Turns a CamelCase string to an under_scored one. +*/ +function CamelCaseToUnderscore($string) +{ + $string = preg_replace('/([A-Z]+)([A-Z][a-z])/','\1_\2',$string); + $string = preg_replace('/([a-z])([A-Z])/','\1_\2', $string); + return strtolower($string); +} + +/*! + Creates a random string of length |$length|, or a random length between 20 + and 100 if NULL. +*/ +function Random($length = NULL) +{ + if ($length === NULL) + $length = rand(20, 100); + + $string = ''; + for ($i = 0; $i < $length; $i++) { + $type = rand(0, 300); + if ($type < 100) + $string .= rand(0, 9); + else if ($type < 200) + $string .= chr(rand(65, 90)); + else + $string .= chr(rand(97, 122)); + } + + return $string; +} -- 2.43.5