JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix db_get_value after mysql->mysqli upgrade
[wfpl.git] / format.php
1 <?php
2
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/
7
8
9 # This file contains basic encodings
10
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);
14         $str = ucwords($str);
15         return $str;
16 }
17
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().");
22         }
23
24         foreach($GLOBALS[$name . '_options']['options'] as $keyval) {
25                 list($key, $value) = $keyval;
26                 if($str == $key) {
27                         return $str;
28                 }
29         }
30
31         return '';
32 }
33
34 function format_int($str) {
35         $str = preg_replace('|[^0-9]|', '', $str);
36         return preg_replace('|^0*([0-9])|', '\1', $str);
37 }
38
39 # format the digits after the decimal point
40 function format_decimals($str) {
41         $str = preg_replace('|[^0-9]|', '', $str);
42         if(strlen($str)) {
43                 $str = substr($str, 0, 1) . preg_replace('|0*$|', '', substr($str, 1));
44         }
45         return $str;
46 }
47
48 function _format_positive_decimal($str) {
49         $str = preg_replace('|[^0-9.]|', '', $str);
50         $pos = strpos($str, '.');
51         if($pos !== false) {
52                 $str = str_replace('.', '', $str);
53                 if($pos == 0) {
54                         return '0.' . format_decimals($str);
55                 } elseif($pos == strlen($str)) {
56                         return format_int($str);
57                 } else {
58                         return format_int(substr($str, 0, $pos)) . '.' . format_decimals(substr($str, $pos));
59                 }
60         }
61         return format_int($str);
62 }
63
64 function format_positive_decimal($str) {
65         $str = _format_positive_decimal($str);
66         if(substr($str, -2) === '.0') {
67                 $str = substr($str, 0, -2);
68         }
69         return $str;
70 }
71
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') {
77                         $str = '-' . $str;
78                 }
79                 return $str;
80         } else {
81                 return format_positive_decimal($str);
82         }
83 }
84
85 # return 0 of there's no digits
86 function format_int_0($str) {
87         $str = format_int($str);
88         if($str == '') {
89                 return '0';
90         }
91         return $str;
92 }
93
94 # USA zip codes
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);
99         }
100         return $str;
101 }
102
103 function format_filename($str, $allow_uppercase = false) {
104         if(!$allow_uppercase) {
105                 $str = strtolower($str);
106         }
107         $str = preg_replace('|[^a-z0-9_.-]|i', '_', $str);
108         return preg_replace('|^[.-]|', '_', $str);
109 }
110
111 function format_path($str, $allow_uppercase = false) {
112         if(!$allow_uppercase) {
113                 $str = strtolower($str);
114         }
115         $str = preg_replace('|[^a-z0-9_./-]|i', '_', $str);
116         return preg_replace('|^[.-]|', '_', $str);
117 }
118
119 function client_path_to_filename($path) {
120         $filename = preg_replace("|.*[:/\\\\]|", '', $path);
121         return format_filename($filename, true);
122 }
123
124
125 function format_image_w_h($str) {
126         $fields = explode(' ', $str);
127         if(count($fields) != 3) {
128                 return '';
129         }
130
131         list($filename, $width, $height) = $fields;
132         $filename = format_path($filename);
133         $width = format_int_0($width);
134         $height = format_int_0($height);
135
136         return "$filename $width $height";
137 }
138
139 function format_image_w_h_thumb_w_h($str) {
140         $fields = explode(' ', $str);
141         if(count($fields) != 6) {
142                 return '';
143         }
144
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);
152
153         return "$filename $width $height $thumb_filename $thumb_width $thumb_height";
154 }
155
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, '-');
161 }
162
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);
169 }
170
171 function format_oneline($str) {
172         $str = str_replace("\r", '', $str);
173         return str_replace("\n", '', $str);
174 }
175
176 function format_unix($str) {
177         return unix_newlines($str);
178 }
179
180 function format_bool($str) {
181         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false' && $str !== 'no' && $str !== 'N' &&  $str !== 'n') {
182                 return 1;
183         } else {
184                 return 0;
185         }
186 }
187
188 function format_yesno($str) {
189         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false' && $str !== 'no' && $str !== 'N' &&  $str !== 'n') {
190                 return 'Yes';
191         } else {
192                 return 'No';
193         }
194 }
195
196 function format_email($str) {
197         # FIXME
198         return trim(format_oneline($str));
199 }
200
201 function format_url($str) {
202         # FIXME check for TLD? encode special chars?
203         $str = trim(format_oneline($str));
204         if($str !== '') {
205                 if(strpos($str, ':') === false) {
206                         $str = 'http://' . $str;
207                 }
208         }
209         return $str;
210 }
211
212 # pass a string containing only numeric digits.
213 # returns a string containing only numeric digits which is 1 greater.
214 function strplusone($str) {
215         $ret = '';
216         $a = str_split($str);
217         $carry = 1;
218         while($a) {
219                 $digit = array_pop($a);
220                 $digit += $carry;
221                 $carry = 0;
222                 if($digit == 10) {
223                         $carry = 1;
224                         $digit = 0;
225                 }
226                 $ret = "$digit$ret";
227         }
228         if($carry) {
229                 $ret = "$carry$ret";
230         }
231
232         return $ret;
233 }
234
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);
239         if($str == '') {
240                 $str = '0';
241         }
242         if(strpos($str, '.')) {
243                 list($int, $decimals) = explode('.', $str);
244                 if(strlen($decimals) == 1) {
245                         $decimals .= '0';
246                 }
247                 if($display_cents) {
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') {
253                                                 $decimals = '00';
254                                                 $int = strplusone($int);
255                                         }
256                                 } else {
257                                         $decimals = substr($decimals, 0, 2);
258                                 }
259                         }
260                         $cents = ".$decimals";
261                 } else {
262                         if(substr($decimals, 0, 1) >= 5) {
263                                 $int = strplusone($int);
264                         }
265                 }
266         } else {
267                 $int = $str;
268                 if($display_cents) {
269                         $cents = '.00';
270                 }
271         }
272         $chars = str_split($int);
273         $output = '';
274         $comma = 4;
275         while($chars) {
276                 --$comma;
277                 if($comma == 0) {
278                         $comma = 3;
279                         $output = ',' . $output;
280                 }
281                 $char = array_pop($chars);
282                 $output = $char . $output;
283         }
284         $output = '$' . $output . $cents;
285         return $output;
286 }
287
288
289 function format_dollars($str) {
290         return format_money($str, false);
291 }
292
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));
298 }
299
300 # date is yyyy-mm-dd
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);
305 }
306
307 # takes any of: HH  :MM  HH:MM
308 # returns decimal number of hours
309 #
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) {
313                 return $str;
314         }
315         $pos = strpos($str, ':');
316         if($pos === false) {
317                 $hours = format_int_0($str);
318                 $minutes = 0;
319         } elseif($pos == 0) {
320                 $hours = 0;
321                 $minutes = format_int_0($str);
322         } else {
323                 $hours = format_int_0(substr($str, 0, $pos));
324                 $minutes = format_int_0(substr($str, $pos + 1));
325         }
326         return $hours + ($minutes / 60.0);
327 }
328
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) {
333                 return $str;
334         }
335         if(strpos($str, ':') !== false) {
336                 return format_hours_minutes($str);
337         } else {
338                 return format_decimal($str);
339         }
340 }
341
342 # takes eg 12:23am
343 # returns decimal number of hours since midnight
344 function format_12hr_to_hours($str) {
345         if(preg_match('|noon|i', $str) === 1) {
346                 return 12;
347         }
348         $hours = format_hours($str);
349         if($hours < 12 && preg_match('|p|i', $str) === 1) {
350                 $hours += 12;
351         }
352         return $hours;
353 }
354
355
356 function format_phone($str) {
357         $str = preg_replace('|[^0-9]|', '', $str);
358         $str = preg_replace('|^1*|', '', $str);
359         $len = strlen($str);
360         $output = '';
361
362         if($len < 10 && $len != 7) {
363                 #NOT A VALID PHONE NUMBER
364                 return $str;
365         }
366
367         if($len > 10) {
368                 $output = ' ext: ' . substr($str, 10);
369                 $len = 10;
370         }
371
372         if($len == 10) {
373                 $area = substr($str, 0, 3);
374                 $str = substr($str, 3);
375         }
376
377         $output = substr($str, 3) . $output;
378         $output = substr($str, 0, 3) . '-' . $output;
379
380         if($area) {
381                 $output = "($area) " . $output;
382         }
383
384         return $output;
385 }