JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
clean up my urls
[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 = ereg_replace('[^0-9]', '', $str);
46         return ereg_replace('^0*([0-9])', '\1', $str);
47 }
48
49 # format the digits after the decimal point
50 function format_decimals($str) {
51         $str = ereg_replace('[^0-9]', '', $str);
52         if(strlen($str)) {
53                 $str = substr($str, 0, 1) . ereg_replace('0*$', '', substr($str, 1));
54         }
55         return $str;
56 }
57
58 function _format_positive_decimal($str) {
59         $str = ereg_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 = ereg_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 = ereg_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 = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
118         return ereg_replace('^[.-]', '_', $str);
119 }
120
121 function format_path($str, $allow_uppercase = false) {
122         if(!$allow_uppercase) {
123                 $str = strtolower($str);
124         }
125         $str = ereg_replace('[^a-zA-Z0-9_./-]', '_', $str);
126         return ereg_replace('^[.-]', '_', $str);
127 }
128
129 function client_path_to_filename($path) {
130         $filename = ereg_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('/[^a-z0-9_]+/', '_', $str);
177         return preg_replace('/^([^a-z_])/', "_\\1", $str);
178 }
179
180 function format_oneline($str) {
181         $str = str_replace("\r", '', $str);
182         return str_replace("\n", '', $str);
183 }
184
185 function format_unix($str) {
186         return unix_newlines($str);
187 }
188
189 function format_bool($str) {
190         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false' && $str !== 'no' && $str !== 'N' &&  $str !== 'n') {
191                 return 1;
192         } else {
193                 return 0;
194         }
195 }
196
197 function format_yesno($str) {
198         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false' && $str !== 'no' && $str !== 'N' &&  $str !== 'n') {
199                 return 'Yes';
200         } else {
201                 return 'No';
202         }
203 }
204
205 function format_email($str) {
206         # FIXME
207         return trim(format_oneline($str));
208 }
209
210 function format_url($str) {
211         # FIXME check for TLD? encode special chars?
212         $str = trim(format_oneline($str));
213         if($str !== '') {
214                 if(strpos($str, ':') === false) {
215                         $str = 'http://' . $str;
216                 }
217         }
218         return $str;
219 }
220
221 # pass a string containing only numeric digits.
222 # returns a string containing only numeric digits which is 1 greater.
223 function strplusone($str) {
224         $ret = '';
225         $a = str_split($str);
226         $carry = 1;
227         while($a) {
228                 $digit = array_pop($a);
229                 $digit += $carry;
230                 $carry = 0;
231                 if($digit == 10) {
232                         $carry = 1;
233                         $digit = 0;
234                 }
235                 $ret = "$digit$ret";
236         }
237         if($carry) {
238                 $ret = "$carry$ret";
239         }
240
241         return $ret;
242 }
243
244 # rounds properly to the nearest penny (or dollar if you pass false for the 2nd
245 # parameter) and prints like so: $12,456.79 or $12,457
246 function format_money($str, $display_cents = true) {
247         $str = format_decimal($str);
248         if($str == '') {
249                 $str = '0';
250         }
251         if(strpos($str, '.')) {
252                 list($int, $decimals) = explode('.', $str);
253                 if(strlen($decimals) == 1) {
254                         $decimals .= '0';
255                 }
256                 if($display_cents) {
257                         if(strlen($decimals) > 2) {
258                                 # round up to the nearest penny
259                                 if(substr($decimals, 2, 1) >= 5) {
260                                         $decimals = strplusone(substr($decimals, 0, 2));
261                                         if($decimals == '100') {
262                                                 $decimals = '00';
263                                                 $int = strplusone($int);
264                                         }
265                                 } else {
266                                         $decimals = substr($decimals, 0, 2);
267                                 }
268                         }
269                         $cents = ".$decimals";
270                 } else {
271                         if(substr($decimals, 0, 1) >= 5) {
272                                 $int = strplusone($int);
273                         }
274                 }
275         } else {
276                 $int = $str;
277                 if($display_cents) {
278                         $cents = '.00';
279                 }
280         }
281         $chars = str_split($int);
282         $output = '';
283         $comma = 4;
284         while($chars) {
285                 --$comma;
286                 if($comma == 0) {
287                         $comma = 3;
288                         $output = ',' . $output;
289                 }
290                 $char = array_pop($chars);
291                 $output = $char . $output;
292         }
293         $output = '$' . $output . $cents;
294         return $output;
295 }
296
297
298 function format_dollars($str) {
299         return format_money($str, false);
300 }
301
302 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
303 function format_mdy_to_ymd($str) {
304         if($str == '') return '';
305         require_once(__DIR__.'/'.'time.php');
306         return mdy_to_ymd(format_oneline($str));
307 }
308
309 # date is yyyy-mm-dd
310 function format_ymd($str) {
311         require_once(__DIR__.'/'.'time.php');
312         list($year, $month, $day) = ymd_clean($str);
313         return sprintf('%04u-%02u-%02u', $year, $month, $day);
314 }
315
316 # takes any of: HH  :MM  HH:MM
317 # returns decimal number of hours
318 #
319 # You probably want to use format_hours() instead because it handles hours with a decimal point.
320 function format_hours_minutes($str) {
321         if(strlen($str) == 0) {
322                 return $str;
323         }
324         $pos = strpos($str, ':');
325         if($pos === false) {
326                 $hours = format_int_0($str);
327                 $minutes = 0;
328         } elseif($pos == 0) {
329                 $hours = 0;
330                 $minutes = format_int_0($str);
331         } else {
332                 $hours = format_int_0(substr($str, 0, $pos));
333                 $minutes = format_int_0(substr($str, $pos + 1));
334         }
335         return $hours + ($minutes / 60.0);
336 }
337
338 # takes any of: HH  :MM  HH:MM  HH.hh(decimal hours)
339 # returns decimal number of hours
340 function format_hours($str) {
341         if(strlen($str) == 0) {
342                 return $str;
343         }
344         if(strpos($str, ':') !== false) {
345                 return format_hours_minutes($str);
346         } else {
347                 return format_decimal($str);
348         }
349 }
350
351 # takes eg 12:23am
352 # returns decimal number of hours since midnight
353 function format_12hr_to_hours($str) {
354         if(eregi('noon', $str)) {
355                 return 12;
356         }
357         $hours = format_hours($str);
358         if($hours < 12 && eregi('p', $str)) {
359                 $hours += 12;
360         }
361         return $hours;
362 }
363
364
365 function format_phone($str) {
366         $str = ereg_replace('[^0-9]', '', $str);
367         $str = ereg_replace('^1*', '', $str);
368         $len = strlen($str);
369         $output = '';
370
371         if($len < 10 && $len != 7) {
372                 #NOT A VALID PHONE NUMBER
373                 return $str;
374         }
375
376         if($len > 10) {
377                 $output = ' ext: ' . substr($str, 10);
378                 $len = 10;
379         }
380
381         if($len == 10) {
382                 $area = substr($str, 0, 3);
383                 $str = substr($str, 3);
384         }
385
386         $output = substr($str, 3) . $output;
387         $output = substr($str, 0, 3) . '-' . $output;
388
389         if($area) {
390                 $output = "($area) " . $output;
391         }
392
393         return $output;
394 }
395
396 ?>