3 # Copyright (C) 2005 Jason Woofenden
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.
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.
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/>.
19 # This file contains basic encodings
21 function format_caption($str) {
22 $str = str_replace('_', ' ', $str);
24 return str_replace('Email', 'E-mail', $str);
27 function format_int($str) {
28 $str = ereg_replace('[^0-9]', '', $str);
29 return ereg_replace('^0*([0-9])', '\1', $str);
32 # format the digits after the decimal point
33 function format_decimals($str) {
34 $str = ereg_replace('[^0-9]', '', $str);
36 $str = substr($str, 0, 1) . ereg_replace('0*$', '', substr($str, 1));
41 function _format_positive_decimal($str) {
42 $str = ereg_replace('[^0-9.]', '', $str);
43 $pos = strpos($str, '.');
45 $str = str_replace('.', '', $str);
47 return '0.' . format_decimals($str);
48 } elseif($pos == strlen($str)) {
49 return format_int($str);
51 return format_int(substr($str, 0, $pos)) . '.' . format_decimals(substr($str, $pos));
54 return format_int($str);
57 function format_positive_decimal($str) {
58 $str = _format_positive_decimal($str);
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') {
74 return format_positive_decimal($str);
78 # return 0 of there's no digits
79 function format_int_0($str) {
80 $str = format_int($str);
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);
96 function format_filename($str, $allow_uppercase = false) {
97 if(!$allow_uppercase) {
98 $str = strtolower($str);
100 $str = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
101 return ereg_replace('^[.-]', '_', $str);
104 function format_path($str, $allow_uppercase = false) {
105 if(!$allow_uppercase) {
106 $str = strtolower($str);
108 $str = ereg_replace('[^a-zA-Z0-9_./-]', '_', $str);
109 return ereg_replace('^[.-]', '_', $str);
112 function client_path_to_filename($path) {
113 $filename = ereg_replace(".*[:/\\]", '', $path);
114 return format_filename($filename, true);
118 function format_image_w_h($str) {
119 $fields = explode(' ', $str);
120 if(count($fields) != 3) {
124 list($filename, $width, $height) = $fields;
125 $filename = format_path($filename);
126 $width = format_int_0($width);
127 $height = format_int_0($height);
129 return "$filename $width $height";
132 function format_image_w_h_thumb_w_h($str) {
133 $fields = explode(' ', $str);
134 if(count($fields) != 6) {
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);
146 return "$filename $width $height $thumb_filename $thumb_width $thumb_height";
149 function format_varname($str) {
150 $str = strtolower($str);
151 $str = ereg_replace('[^a-z0-9_]', '_', $str);
152 return ereg_replace('^[0-9]*', '', $str);
155 function format_oneline($str) {
156 $str = str_replace("\r", '', $str);
157 return str_replace("\n", '', $str);
160 function format_unix($str) {
161 return unix_newlines($str);
164 function format_bool($str) {
165 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
172 function format_yesno($str) {
173 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
180 function format_email($str) {
182 return trim(format_oneline($str));
185 function format_url($str) {
186 # FIXME check for TLD? encode special chars?
187 $str = trim(format_oneline($str));
189 if(strpos($str, ':') === false) {
190 $str = 'http://' . $str;
196 # pass a string containing only numeric digits.
197 # returns a string containing only numeric digits which is 1 greater.
198 function strplusone($str) {
200 $a = str_split($str);
203 $digit = array_pop($a);
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);
226 if(strpos($str, '.')) {
227 list($int, $decimals) = explode('.', $str);
228 if(strlen($decimals) == 1) {
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') {
238 $int = strplusone($int);
241 $decimals = substr($decimals, 0, 2);
244 $cents = ".$decimals";
246 if(substr($decimals, 0, 1) >= 5) {
247 $int = strplusone($int);
256 $chars = str_split($int);
263 $output = ',' . $output;
265 $char = array_pop($chars);
266 $output = $char . $output;
268 $output = '$' . $output . $cents;
273 function format_dollars($str) {
274 return format_money($str, false);
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));
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);
291 # takes any of: HH :MM HH:MM
292 # returns decimal number of hours
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) {
299 $pos = strpos($str, ':');
301 $hours = format_int_0($str);
303 } elseif($pos == 0) {
305 $minutes = format_int_0($str);
307 $hours = format_int_0(substr($str, 0, $pos));
308 $minutes = format_int_0(substr($str, $pos + 1));
310 return $hours + ($minutes / 60.0);
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) {
319 if(strpos($str, ':') !== false) {
320 return format_hours_minutes($str);
322 return format_decimal($str);
327 # returns decimal number of hours since midnight
328 function format_12hr_to_hours($str) {
329 if(eregi('noon', $str)) {
332 $hours = format_hours($str);
333 if($hours < 12 && eregi('p', $str)) {
340 function format_phone($str) {
341 $str = ereg_replace('[^0-9]', '', $str);
342 $str = ereg_replace('^1*', '', $str);
346 if($len < 10 && $len != 7) {
347 #NOT A VALID PHONE NUMBER
352 $output = ' ext: ' . substr($str, 10);
357 $area = substr($str, 0, 3);
358 $str = substr($str, 3);
361 $output = substr($str, 3) . $output;
362 $output = substr($str, 0, 3) . '-' . $output;
365 $output = "($area) " . $output;