JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added code for 12 hour time formatting. Moved enc_mdy and format_ymd out of time.php
[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         return ereg_replace('^([0-9])0*', '\1', $str);
51 }
52
53 function _format_positive_decimal($str) {
54         $str = ereg_replace('[^0-9.]', '', $str);
55         $pos = strpos($str, '.');
56         if($pos !== false) {
57                 $str = str_replace('.', '', $str);
58                 if($pos == 0) {
59                         return '0.' . format_decimals($str);
60                 } elseif($pos == strlen($str)) {
61                         return format_int($str);
62                 } else {
63                         return format_int(substr($str, 0, $pos)) . '.' . format_decimals(substr($str, $pos));
64                 }
65         }
66         return format_int($str);
67 }
68
69 function format_positive_decimal($str) {
70         $str = _format_positive_decimal($str);
71         if($str === '0.0') {
72                 $str = '0';
73         }
74         return $str;
75 }
76
77 function format_decimal($str) {
78         $str = ereg_replace('[^0-9.-]', '', $str);
79         if(substr($str, 0, 1) == '-') {
80                 $str = format_positive_decimal(substr($str, 1));
81                 if($str !== '' && $str !== '0') {
82                         $str = '-' . $str;
83                 }
84                 return $str;
85         } else {
86                 return format_positive_decimal($str);
87         }
88 }
89
90 # return 0 of there's no digits
91 function format_int_0($str) {
92         $str = format_int($str);
93         if($str == '') {
94                 return '0';
95         }
96         return $str;
97 }
98
99 function format_zip($str) {
100         $str = ereg_replace('[^0-9]', '', $str);
101         if(strlen($str) > 5) {
102                 return substr($str, 0, 5) . '-' . substr($str, 5);
103         }
104         return $str;
105 }
106
107 function format_filename($str, $allow_uppercase = false) {
108         if(!$allow_uppercase) {
109                 $str = strtolower($str);
110         }
111         $str = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
112         return ereg_replace('^[.-]', '_', $str);
113 }
114
115 function client_path_to_filename($path) {
116         $filename = ereg_replace(".*[:/\\]", '', $path);
117         return format_filename($filename, true);
118 }
119
120
121 function format_h_w_image($str) {
122         $fields = explode(' ', $str);
123         if(count($fields) != 3) {
124                 return '';
125         }
126
127         list($width, $height, $filename) = $fields;
128         $width = format_int_0($width);
129         $height = format_int_0($height);
130         $filename = format_filename($filename);
131
132         return "$width $height $filename";
133 }
134
135 function format_varname($str) {
136         $str = strtolower($str);
137         $str = ereg_replace('[^a-z0-9_]', '_', $str);
138         return ereg_replace('^[0-9]*', '', $str);
139 }
140
141 function format_oneline($str) {
142         $str = str_replace("\r", '', $str);
143         return str_replace("\n", '', $str);
144 }
145
146 function format_unix($str) {
147         return unix_newlines($str);
148 }
149
150 function format_bool($str) {
151         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
152                 return 1;
153         } else {
154                 return 0;
155         }
156 }
157
158 function format_yesno($str) {
159         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
160                 return 'Yes';
161         } else {
162                 return 'No';
163         }
164 }
165
166 function format_email($str) {
167         # FIXME
168         return trim(format_oneline($str));
169 }
170
171 function format_url($str) {
172         # FIXME check for TLD? encode special chars?
173         $str = trim(format_oneline($str));
174         if($str !== '') {
175                 if(strpos($str, ':') === false) {
176                         $str = 'http://' . $str;
177                 }
178         }
179         return $str;
180 }
181
182 function format_money($str, $display_cents = true) {
183         $str = ereg_replace('[^0-9.]', '', $str);
184         if($display_cents) {
185                 $int = (int)($str * 100);
186                 $cents = $int % 100;
187                 $cents = sprintf('.%02d', $cents);
188                 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
189         } else {
190                 $cents = '';
191                 $int = round($str);
192         }
193         $chars = (string)$int;
194         $output = '';
195         $comma = 4;
196         $index = strlen($chars);
197         while($index) {
198                 --$index;
199                 --$comma;
200                 if($comma == 0) {
201                         $comma = 3;
202                         $output = ',' . $output;
203                 }
204                 $char = substr($chars, $index, 1);
205                 $output = $char . $output;
206         }
207         $output = '$' . $output . $cents;
208         return $output;
209 }
210
211 function format_dollars($str) {
212         return format_money($str, false);
213 }
214
215 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
216 function format_mdy_to_ymd($str) {
217         require_once('code/wfpl/time.php');
218         return mdy_to_ymd(format_oneline($str));
219 }
220
221 # date is yyyy-mm-dd
222 function format_ymd($str) {
223         require_once('code/wfpl/time.php');
224         list($year, $month, $day) = ymd_clean($str);
225         return sprintf('%04u-%02u-%02u', $year, $month, $day);
226 }
227
228 # takes any of: HH  :MM  HH:MM
229 # returns decimal number of hours
230 #
231 # You probably want to use format_hours() instead because it handles hours with a decimal point.
232 function format_hours_minutes($str) {
233         if(strlen($str) == 0) {
234                 return $str;
235         }
236         $pos = strpos($str, ':');
237         if($pos === false) {
238                 $hours = format_int_0($str);
239                 $minutes = 0;
240         } elseif($pos == 0) {
241                 $hours = 0;
242                 $minutes = format_int_0($str);
243         } else {
244                 $hours = format_int_0(substr($str, 0, $pos));
245                 $minutes = format_int_0(substr($str, $pos + 1));
246         }
247         return $hours + ($minutes / 60.0);
248 }
249
250 # takes any of: HH  :MM  HH:MM  HH.hh(decimal hours)
251 # returns decimal number of hours
252 function format_hours($str) {
253         if(strlen($str) == 0) {
254                 return $str;
255         }
256         if(strpos($str, ':') !== false) {
257                 return format_hours_minutes($str);
258         } else {
259                 return format_decimal($str);
260         }
261 }
262
263 # takes eg 12:23am
264 # returns decimal number of hours since midnight
265 function format_12hr_to_hours($str) {
266         if(eregi('noon', $str)) {
267                 return 12;
268         }
269         $hours = format_hours($str);
270         if($hours < 12 && eregi('p', $str)) {
271                 $hours += 12;
272         }
273         return $hours;
274 }
275
276
277 function format_phone($str) {
278         $str = ereg_replace('[^0-9]', '', $str);
279         $str = ereg_replace('^1*', '', $str);
280         $len = strlen($str);
281         $output = '';
282
283         if($len < 10 && $len != 7) {
284                 #NOT A VALID PHONE NUMBER
285                 return $str;
286         }
287
288         if($len > 10) {
289                 $output = ' ext: ' . substr($str, 10);
290                 $len = 10;
291         }
292
293         if($len == 10) {
294                 $area = substr($str, 0, 3);
295                 $str = substr($str, 3);
296         }
297
298         $output = substr($str, 3) . $output;
299         $output = substr($str, 0, 3) . '-' . $output;
300
301         if($area) {
302                 $output = "($area) " . $output;
303         }
304
305         return $output;
306 }
307
308 ?>