JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
enc_htmlbrtab: don't double spaces
[wfpl.git] / format.php
1 <?php
2
3 #  Copyright (C) 2005 Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #  
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #  
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 # This file contains basic encodings
20
21 function format_caption($str) {
22         $str = preg_replace("/([a-z])([A-Z])/", "\\1 \\2", $str); # split words on camelCase
23         $str = str_replace('_', ' ', $str);
24         $str = ucwords($str);
25         return $str;
26 }
27
28 # This function makes sure that $str is in the list of options, and returns "" otherwise
29 function format_options($str, $name) {
30         if(!isset($GLOBALS[$name . '_options'])) {
31                 die("Couldn't find options for \"$name\". Be sure to call pulldown().");
32         }
33
34         foreach($GLOBALS[$name . '_options']['options'] as $keyval) {
35                 list($key, $value) = $keyval;
36                 if($str == $key) {
37                         return $str;
38                 }
39         }
40
41         return '';
42 }
43
44 function format_int($str) {
45         $str = preg_replace('|[^0-9]|', '', $str);
46         return preg_replace('|^0*([0-9])|', '\1', $str);
47 }
48
49 # format the digits after the decimal point
50 function format_decimals($str) {
51         $str = preg_replace('|[^0-9]|', '', $str);
52         if(strlen($str)) {
53                 $str = substr($str, 0, 1) . preg_replace('|0*$|', '', substr($str, 1));
54         }
55         return $str;
56 }
57
58 function _format_positive_decimal($str) {
59         $str = preg_replace('|[^0-9.]|', '', $str);
60         $pos = strpos($str, '.');
61         if($pos !== false) {
62                 $str = str_replace('.', '', $str);
63                 if($pos == 0) {
64                         return '0.' . format_decimals($str);
65                 } elseif($pos == strlen($str)) {
66                         return format_int($str);
67                 } else {
68                         return format_int(substr($str, 0, $pos)) . '.' . format_decimals(substr($str, $pos));
69                 }
70         }
71         return format_int($str);
72 }
73
74 function format_positive_decimal($str) {
75         $str = _format_positive_decimal($str);
76         if(substr($str, -2) === '.0') {
77                 $str = substr($str, 0, -2);
78         }
79         return $str;
80 }
81
82 function format_decimal($str) {
83         $str = preg_replace('|[^0-9.-]|', '', $str);
84         if(substr($str, 0, 1) == '-') {
85                 $str = format_positive_decimal(substr($str, 1));
86                 if($str !== '' && $str !== '0') {
87                         $str = '-' . $str;
88                 }
89                 return $str;
90         } else {
91                 return format_positive_decimal($str);
92         }
93 }
94
95 # return 0 of there's no digits
96 function format_int_0($str) {
97         $str = format_int($str);
98         if($str == '') {
99                 return '0';
100         }
101         return $str;
102 }
103
104 # USA zip codes
105 function format_zip($str) {
106         $str = preg_replace('|[^0-9]|', '', $str);
107         if(strlen($str) > 5) {
108                 return substr($str, 0, 5) . '-' . substr($str, 5);
109         }
110         return $str;
111 }
112
113 function format_filename($str, $allow_uppercase = false) {
114         if(!$allow_uppercase) {
115                 $str = strtolower($str);
116         }
117         $str = preg_replace('|[^a-z0-9_.-]|i', '_', $str);
118         return preg_replace('|^[.-]|', '_', $str);
119 }
120
121 function format_path($str, $allow_uppercase = false) {
122         if(!$allow_uppercase) {
123                 $str = strtolower($str);
124         }
125         $str = preg_replace('|[^a-z0-9_./-]|i', '_', $str);
126         return preg_replace('|^[.-]|', '_', $str);
127 }
128
129 function client_path_to_filename($path) {
130         $filename = preg_replace("|.*[:/\\\\]|", '', $path);
131         return format_filename($filename, true);
132 }
133
134
135 function format_image_w_h($str) {
136         $fields = explode(' ', $str);
137         if(count($fields) != 3) {
138                 return '';
139         }
140
141         list($filename, $width, $height) = $fields;
142         $filename = format_path($filename);
143         $width = format_int_0($width);
144         $height = format_int_0($height);
145
146         return "$filename $width $height";
147 }
148
149 function format_image_w_h_thumb_w_h($str) {
150         $fields = explode(' ', $str);
151         if(count($fields) != 6) {
152                 return '';
153         }
154
155         list($filename, $width, $height, $thumb_filename, $thumb_width, $thumb_height) = $fields;
156         $filename = format_path($filename);
157         $width = format_int_0($width);
158         $height = format_int_0($height);
159         $thumb_filename = format_path($thumb_filename);
160         $thumb_width = format_int_0($thumb_width);
161         $thumb_height = format_int_0($thumb_height);
162
163         return "$filename $width $height $thumb_filename $thumb_width $thumb_height";
164 }
165
166 function format_slug($str) {
167         $str = strtolower($str);
168         $str = str_replace("'", '', $str);
169         $str = preg_replace('/[^a-z0-9-]+/', '-', $str);
170         return trim($str, '-');
171 }
172
173 function format_varname($str) {
174         $str = preg_replace("/([a-z])([A-Z])/", "\\1_\\2", $str); # split words on camelCase
175         $str = strtolower($str);
176         $str = preg_replace("/['‘’]/", '', $str);
177         $str = preg_replace('/[^a-z0-9_]+/', '_', $str);
178         return preg_replace('/^([^a-z_])/', "_\\1", $str);
179 }
180
181 function format_oneline($str) {
182         $str = str_replace("\r", '', $str);
183         return str_replace("\n", '', $str);
184 }
185
186 function format_unix($str) {
187         return unix_newlines($str);
188 }
189
190 function format_bool($str) {
191         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false' && $str !== 'no' && $str !== 'N' &&  $str !== 'n') {
192                 return 1;
193         } else {
194                 return 0;
195         }
196 }
197
198 function format_yesno($str) {
199         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false' && $str !== 'no' && $str !== 'N' &&  $str !== 'n') {
200                 return 'Yes';
201         } else {
202                 return 'No';
203         }
204 }
205
206 function format_email($str) {
207         # FIXME
208         return trim(format_oneline($str));
209 }
210
211 function format_url($str) {
212         # FIXME check for TLD? encode special chars?
213         $str = trim(format_oneline($str));
214         if($str !== '') {
215                 if(strpos($str, ':') === false) {
216                         $str = 'http://' . $str;
217                 }
218         }
219         return $str;
220 }
221
222 # pass a string containing only numeric digits.
223 # returns a string containing only numeric digits which is 1 greater.
224 function strplusone($str) {
225         $ret = '';
226         $a = str_split($str);
227         $carry = 1;
228         while($a) {
229                 $digit = array_pop($a);
230                 $digit += $carry;
231                 $carry = 0;
232                 if($digit == 10) {
233                         $carry = 1;
234                         $digit = 0;
235                 }
236                 $ret = "$digit$ret";
237         }
238         if($carry) {
239                 $ret = "$carry$ret";
240         }
241
242         return $ret;
243 }
244
245 # rounds properly to the nearest penny (or dollar if you pass false for the 2nd
246 # parameter) and prints like so: $12,456.79 or $12,457
247 function format_money($str, $display_cents = true) {
248         $str = format_decimal($str);
249         if($str == '') {
250                 $str = '0';
251         }
252         if(strpos($str, '.')) {
253                 list($int, $decimals) = explode('.', $str);
254                 if(strlen($decimals) == 1) {
255                         $decimals .= '0';
256                 }
257                 if($display_cents) {
258                         if(strlen($decimals) > 2) {
259                                 # round up to the nearest penny
260                                 if(substr($decimals, 2, 1) >= 5) {
261                                         $decimals = strplusone(substr($decimals, 0, 2));
262                                         if($decimals == '100') {
263                                                 $decimals = '00';
264                                                 $int = strplusone($int);
265                                         }
266                                 } else {
267                                         $decimals = substr($decimals, 0, 2);
268                                 }
269                         }
270                         $cents = ".$decimals";
271                 } else {
272                         if(substr($decimals, 0, 1) >= 5) {
273                                 $int = strplusone($int);
274                         }
275                 }
276         } else {
277                 $int = $str;
278                 if($display_cents) {
279                         $cents = '.00';
280                 }
281         }
282         $chars = str_split($int);
283         $output = '';
284         $comma = 4;
285         while($chars) {
286                 --$comma;
287                 if($comma == 0) {
288                         $comma = 3;
289                         $output = ',' . $output;
290                 }
291                 $char = array_pop($chars);
292                 $output = $char . $output;
293         }
294         $output = '$' . $output . $cents;
295         return $output;
296 }
297
298
299 function format_dollars($str) {
300         return format_money($str, false);
301 }
302
303 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
304 function format_mdy_to_ymd($str) {
305         if($str == '') return '';
306         require_once(__DIR__.'/'.'time.php');
307         return mdy_to_ymd(format_oneline($str));
308 }
309
310 # date is yyyy-mm-dd
311 function format_ymd($str) {
312         require_once(__DIR__.'/'.'time.php');
313         list($year, $month, $day) = ymd_clean($str);
314         return sprintf('%04u-%02u-%02u', $year, $month, $day);
315 }
316
317 # takes any of: HH  :MM  HH:MM
318 # returns decimal number of hours
319 #
320 # You probably want to use format_hours() instead because it handles hours with a decimal point.
321 function format_hours_minutes($str) {
322         if(strlen($str) == 0) {
323                 return $str;
324         }
325         $pos = strpos($str, ':');
326         if($pos === false) {
327                 $hours = format_int_0($str);
328                 $minutes = 0;
329         } elseif($pos == 0) {
330                 $hours = 0;
331                 $minutes = format_int_0($str);
332         } else {
333                 $hours = format_int_0(substr($str, 0, $pos));
334                 $minutes = format_int_0(substr($str, $pos + 1));
335         }
336         return $hours + ($minutes / 60.0);
337 }
338
339 # takes any of: HH  :MM  HH:MM  HH.hh(decimal hours)
340 # returns decimal number of hours
341 function format_hours($str) {
342         if(strlen($str) == 0) {
343                 return $str;
344         }
345         if(strpos($str, ':') !== false) {
346                 return format_hours_minutes($str);
347         } else {
348                 return format_decimal($str);
349         }
350 }
351
352 # takes eg 12:23am
353 # returns decimal number of hours since midnight
354 function format_12hr_to_hours($str) {
355         if(preg_match('|noon|i', $str) === 1) {
356                 return 12;
357         }
358         $hours = format_hours($str);
359         if($hours < 12 && preg_match('|p|i', $str) === 1) {
360                 $hours += 12;
361         }
362         return $hours;
363 }
364
365
366 function format_phone($str) {
367         $str = preg_replace('|[^0-9]|', '', $str);
368         $str = preg_replace('|^1*|', '', $str);
369         $len = strlen($str);
370         $output = '';
371
372         if($len < 10 && $len != 7) {
373                 #NOT A VALID PHONE NUMBER
374                 return $str;
375         }
376
377         if($len > 10) {
378                 $output = ' ext: ' . substr($str, 10);
379                 $len = 10;
380         }
381
382         if($len == 10) {
383                 $area = substr($str, 0, 3);
384                 $str = substr($str, 3);
385         }
386
387         $output = substr($str, 3) . $output;
388         $output = substr($str, 0, 3) . '-' . $output;
389
390         if($area) {
391                 $output = "($area) " . $output;
392         }
393
394         return $output;
395 }
396
397 ?>