3 # This program is in the public domain within the United States. Additionally,
4 # we waive copyright and related rights in the work worldwide through the CC0
5 # 1.0 Universal public domain dedication, which can be found at
6 # http://creativecommons.org/publicdomain/zero/1.0/
9 # This file contains basic encodings
11 function format_caption($str) {
12 $str = preg_replace("/([a-z])([A-Z])/", "\\1 \\2", $str); # split words on camelCase
13 $str = str_replace('_', ' ', $str);
18 # This function makes sure that $str is in the list of options, and returns "" otherwise
19 function format_options($str, $name) {
20 if(!isset($GLOBALS[$name . '_options'])) {
21 die("Couldn't find options for \"$name\". Be sure to call pulldown().");
24 foreach($GLOBALS[$name . '_options']['options'] as $keyval) {
25 list($key, $value) = $keyval;
34 function format_int($str) {
35 $str = preg_replace('|[^0-9]|', '', $str);
36 return preg_replace('|^0*([0-9])|', '\1', $str);
39 # format the digits after the decimal point
40 function format_decimals($str) {
41 $str = preg_replace('|[^0-9]|', '', $str);
43 $str = substr($str, 0, 1) . preg_replace('|0*$|', '', substr($str, 1));
48 function _format_positive_decimal($str) {
49 $str = preg_replace('|[^0-9.]|', '', $str);
50 $pos = strpos($str, '.');
52 $str = str_replace('.', '', $str);
54 return '0.' . format_decimals($str);
55 } elseif($pos == strlen($str)) {
56 return format_int($str);
58 return format_int(substr($str, 0, $pos)) . '.' . format_decimals(substr($str, $pos));
61 return format_int($str);
64 function format_positive_decimal($str) {
65 $str = _format_positive_decimal($str);
66 if(substr($str, -2) === '.0') {
67 $str = substr($str, 0, -2);
72 function format_decimal($str) {
73 $str = preg_replace('|[^0-9.-]|', '', $str);
74 if(substr($str, 0, 1) == '-') {
75 $str = format_positive_decimal(substr($str, 1));
76 if($str !== '' && $str !== '0') {
81 return format_positive_decimal($str);
85 # return 0 of there's no digits
86 function format_int_0($str) {
87 $str = format_int($str);
95 function format_zip($str) {
96 $str = preg_replace('|[^0-9]|', '', $str);
97 if(strlen($str) > 5) {
98 return substr($str, 0, 5) . '-' . substr($str, 5);
103 function format_filename($str, $allow_uppercase = false) {
104 if(!$allow_uppercase) {
105 $str = strtolower($str);
107 $str = preg_replace('|[^a-z0-9_.-]|i', '_', $str);
108 return preg_replace('|^[.-]|', '_', $str);
111 function format_path($str, $allow_uppercase = false) {
112 if(!$allow_uppercase) {
113 $str = strtolower($str);
115 $str = preg_replace('|[^a-z0-9_./-]|i', '_', $str);
116 return preg_replace('|^[.-]|', '_', $str);
119 function client_path_to_filename($path) {
120 $filename = preg_replace("|.*[:/\\\\]|", '', $path);
121 return format_filename($filename, true);
125 function format_image_w_h($str) {
126 $fields = explode(' ', $str);
127 if(count($fields) != 3) {
131 list($filename, $width, $height) = $fields;
132 $filename = format_path($filename);
133 $width = format_int_0($width);
134 $height = format_int_0($height);
136 return "$filename $width $height";
139 function format_image_w_h_thumb_w_h($str) {
140 $fields = explode(' ', $str);
141 if(count($fields) != 6) {
145 list($filename, $width, $height, $thumb_filename, $thumb_width, $thumb_height) = $fields;
146 $filename = format_path($filename);
147 $width = format_int_0($width);
148 $height = format_int_0($height);
149 $thumb_filename = format_path($thumb_filename);
150 $thumb_width = format_int_0($thumb_width);
151 $thumb_height = format_int_0($thumb_height);
153 return "$filename $width $height $thumb_filename $thumb_width $thumb_height";
156 function format_slug($str) {
157 $str = strtolower($str);
158 $str = str_replace("'", '', $str);
159 $str = preg_replace('/[^a-z0-9-]+/', '-', $str);
160 return trim($str, '-');
163 function format_varname($str) {
164 $str = preg_replace("/([a-z])([A-Z])/", "\\1_\\2", $str); # split words on camelCase
165 $str = strtolower($str);
166 $str = preg_replace("/['‘’]/", '', $str);
167 $str = preg_replace('/[^a-z0-9_]+/', '_', $str);
168 return preg_replace('/^([^a-z_])/', "_\\1", $str);
171 function format_oneline($str) {
172 $str = str_replace("\r", '', $str);
173 return str_replace("\n", '', $str);
176 function format_unix($str) {
177 return unix_newlines($str);
180 function format_bool($str) {
181 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false' && $str !== 'no' && $str !== 'N' && $str !== 'n') {
188 function format_yesno($str) {
189 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false' && $str !== 'no' && $str !== 'N' && $str !== 'n') {
196 function format_email($str) {
198 return trim(format_oneline($str));
201 function format_url($str) {
202 # FIXME check for TLD? encode special chars?
203 $str = trim(format_oneline($str));
205 if(strpos($str, ':') === false) {
206 $str = 'http://' . $str;
212 # pass a string containing only numeric digits.
213 # returns a string containing only numeric digits which is 1 greater.
214 function strplusone($str) {
216 $a = str_split($str);
219 $digit = array_pop($a);
235 # rounds properly to the nearest penny (or dollar if you pass false for the 2nd
236 # parameter) and prints like so: $12,456.79 or $12,457
237 function format_money($str, $display_cents = true) {
238 $str = format_decimal($str);
242 if(strpos($str, '.')) {
243 list($int, $decimals) = explode('.', $str);
244 if(strlen($decimals) == 1) {
248 if(strlen($decimals) > 2) {
249 # round up to the nearest penny
250 if(substr($decimals, 2, 1) >= 5) {
251 $decimals = strplusone(substr($decimals, 0, 2));
252 if($decimals == '100') {
254 $int = strplusone($int);
257 $decimals = substr($decimals, 0, 2);
260 $cents = ".$decimals";
262 if(substr($decimals, 0, 1) >= 5) {
263 $int = strplusone($int);
272 $chars = str_split($int);
279 $output = ',' . $output;
281 $char = array_pop($chars);
282 $output = $char . $output;
284 $output = '$' . $output . $cents;
289 function format_dollars($str) {
290 return format_money($str, false);
293 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
294 function format_mdy_to_ymd($str) {
295 if($str == '') return '';
296 require_once(__DIR__.'/'.'time.php');
297 return mdy_to_ymd(format_oneline($str));
301 function format_ymd($str) {
302 require_once(__DIR__.'/'.'time.php');
303 list($year, $month, $day) = ymd_clean($str);
304 return sprintf('%04u-%02u-%02u', $year, $month, $day);
307 # takes any of: HH :MM HH:MM
308 # returns decimal number of hours
310 # You probably want to use format_hours() instead because it handles hours with a decimal point.
311 function format_hours_minutes($str) {
312 if(strlen($str) == 0) {
315 $pos = strpos($str, ':');
317 $hours = format_int_0($str);
319 } elseif($pos == 0) {
321 $minutes = format_int_0($str);
323 $hours = format_int_0(substr($str, 0, $pos));
324 $minutes = format_int_0(substr($str, $pos + 1));
326 return $hours + ($minutes / 60.0);
329 # takes any of: HH :MM HH:MM HH.hh(decimal hours)
330 # returns decimal number of hours
331 function format_hours($str) {
332 if(strlen($str) == 0) {
335 if(strpos($str, ':') !== false) {
336 return format_hours_minutes($str);
338 return format_decimal($str);
343 # returns decimal number of hours since midnight
344 function format_12hr_to_hours($str) {
345 if(preg_match('|noon|i', $str) === 1) {
348 $hours = format_hours($str);
349 if($hours < 12 && preg_match('|p|i', $str) === 1) {
356 function format_phone($str) {
357 $str = preg_replace('|[^0-9]|', '', $str);
358 $str = preg_replace('|^1*|', '', $str);
362 if($len < 10 && $len != 7) {
363 #NOT A VALID PHONE NUMBER
368 $output = ' ext: ' . substr($str, 10);
373 $area = substr($str, 0, 3);
374 $str = substr($str, 3);
377 $output = substr($str, 3) . $output;
378 $output = substr($str, 0, 3) . '-' . $output;
381 $output = "($area) " . $output;