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