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