11 * Convert a csv string, or an array of items which
12 * may contain csv strings, into an array of items.
15 * A simple csv string; e.g. 'a,b,c'
16 * or a simple list of items; e.g. array('a','b','c')
17 * or some combination; e.g. array('a,b','c') or array('a,','b,','c,')
20 * A simple list of items (e.g. array('a','b','c')
22 public static function csvToArray($args)
25 // Step 1: implode(',',$args) converts from, say, array('a,','b,','c,') to 'a,,b,,c,'
26 // Step 2: explode(',', ...) converts to array('a','','b','','c','')
27 // Step 3: array_filter(...) removes the empty items
28 // Step 4: array_map(...) trims extra whitespace from each item
29 // (handles csv strings with extra whitespace, e.g. 'a, b, c')
31 return array_map('trim', array_filter(explode(',', is_array($args) ? implode(',', $args) : $args)));
35 * Replace placeholders in a string.
38 * interpolate('Hello, {var}', ['var' => 'world']) ==> 'Hello, world'
39 * interpolate('Do !what', ['!what' => 'work']) ==> 'Do work'
41 * @param string $message
42 * The string with placeholders to be interpolated.
43 * @param array $context
44 * An associative array of values to be inserted into the message.
46 * The resulting string with all placeholders filled in.
48 public static function interpolate($message, array $context = [])
50 // Take no action if there is no context
51 if (empty($context)) {
55 // build a replacement array with braces around the context keys
57 foreach ($context as $key => $val) {
58 if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
59 $replace[static::interpolationKey($key)] = $val;
63 // interpolate replacement values into the message and return
64 return strtr($message, $replace);
68 * Wrap simple strings (with no special characters) in {}s
71 * A key from an interpolation context.
73 * The key prepared for interpolation.
75 private static function interpolationKey($key)
77 if (ctype_alpha($key)) {
78 return sprintf('{%s}', $key);
84 * Replace tilde in a path with the HOME directory.
87 * A path that may contain a ~ at front.
90 * The effective home dir for this request.
92 * @return string The path with tilde replaced, if applicable.
93 * The path with tilde replaced, if applicable.
95 public static function replaceTilde($path, $home)
97 $replacement = $home . '/';
99 if (preg_match($match, $path)) {
100 return preg_replace($match, $replacement, $path);
106 * Generate a random alphanumeric password. Copied from user.module.
112 public static function generatePassword($length = 10)
114 // This variable contains the list of allowable characters for the
115 // password. Note that the number 0 and the letter 'O' have been
116 // removed to avoid confusion between the two. The same is true
117 // of 'I', 1, and 'l'.
118 $allowable_characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
120 // Zero-based count of characters in the allowable list:
121 $len = strlen($allowable_characters) - 1;
123 // Declare the password as a blank string.
126 // Loop the number of times specified by $length.
127 for ($i = 0; $i < $length; $i++) {
128 // Each iteration, pick a random character from the
129 // allowable string and append it to the password:
130 $pass .= $allowable_characters[mt_rand(0, $len)];